接受ActiveMQ信息,通过openfire公告发送给指定用户(二)

package com.nxt.nb.op;


import java.io.File;
import java.io.IOException;
import java.io.StringReader;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


import com.huawei.insa2.util.Args;
import com.huawei.insa2.util.Cfg;
import com.nxt.nb.dao.ResourcePath;


/**
 * 通过openfire将公告发送给用户
 * 
 * @author dwd
 */
public class SendOpenfire_NB {
private static String url = "";
private static String user = "";
private static String pwd = "";
private static SendOpenfire_NB instance;


private static String type = "";
private static String to = "";
private static String from = "";
private static String subject = "";
private static String body = "";


public SendOpenfire_NB() {
// 实例化
Args args = new Args();
try {
args = new Cfg(ResourcePath.realpath + File.separator + "op.xml").getArgs("OPConnect");
url = args.get("Url", "");
user = args.get("User", "");
pwd = args.get("Pwd", "");
} catch (IOException e1) {
e1.printStackTrace();
}


}


public synchronized static SendOpenfire_NB getInstance() {
if (instance == null)
return instance = new SendOpenfire_NB();
return instance;
}


//把公告发送给用户
public void SendOp_NB(String xmlDoc) {
// 连接openfire服务器
XMPPConnection xmmpconnection = new XMPPConnection(url);
try {

xmmpconnection.connect();
//Roster roster = xmmpconnection.getConnection().getRoster();
Roster roster = xmmpconnection.getRoster();
// nowMessage.getTo()=xxx@nx01.6636.net
Presence presence = roster.getPresence("user01@dodo-pc");
            //判断是否在线
if (presence.getType() == Presence.Type.available) {// 在线

}else{
// xmmpconnection.loginAnonymously();// 匿名登录。
xmmpconnection.login(user, pwd);
}

} catch (XMPPException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


StringReader read = new StringReader(xmlDoc);
// 把xml变成输入流
InputSource source = new InputSource(read);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
// 解析xml串内容
Document doc = db.parse(source);
// 得到根节点
Element root = doc.getDocumentElement();
// 获取元素属性内容
type = root.getAttribute("type");
to = root.getAttribute("to");
from = root.getAttribute("from");
msgtype = root.getAttribute("msgtype");
fileType = root.getAttribute("fileType");
msgTime = root.getAttribute("msgTime");


System.out.println("to属性的值" + root.getAttribute("to"));
// 获得次级节点
NodeList collegeNodes = root.getChildNodes();
if (collegeNodes == null)
return;
// 获取元素内容
subject = collegeNodes.item(0).getTextContent();
body = collegeNodes.item(1).getTextContent();
//发送给用户
Message m = new Message();

m.setFrom(from);
m.setTo(to);
m.setSubject(subject);
m.setBody(body);
xmmpconnection.sendPacket(m);


} catch (Exception e) {
e.printStackTrace();
}
}


public static void SendNB() throws XMPPException {
XMPPConnection xmmpconnection = new XMPPConnection(url);
xmmpconnection.connect();
// xmmpconnection.loginAnonymously();// 匿名登录。
xmmpconnection.login(user, pwd);
Message m = new Message();
// m.setFrom(from);
// m.setTo(to);
// m.setSubject(subject);
// m.setBody(body);
xmmpconnection.sendPacket(m);
}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值