1、 消息的发布与订阅,消息的发布是指消息服务器发布一个一条消息,比如通知,其他系统或者应用能够接收到该条消息。
消息的订阅就是指,应用A向服务器订阅一个主题消息,当服务器发布消息时,应用能够收到这条消息。
2、 IBMMQ的使用
要使用IBMMQ,首先需要下它的jar包,见【附件】。
3、 创建连接,Java代码如下:
package sso.gznx.timer;
import java.util.Properties;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import nc.bs.logging.Logger;
import sso.gznx.ParserProperties;
import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQTopicConnection;
import com.ibm.mq.jms.MQTopicConnectionFactory;
import com.ibm.mq.jms.MQTopicPublisher;
import com.ibm.mq.jms.MQTopicSession;
import com.ibm.mq.jms.MQTopicSubscriber;
public classMQTopicUtil {
privatestaticMQTopicConnection connection;
privatestaticMQTopicSession session;
privatestaticMQTopicSubscriber subscriber;
privatestaticMQTopicConnectionFactory tcf;
privatestaticTopic topic;
privatestaticString qmName;
privatestaticString tName;
privatestaticString hostname;
privatestaticString channel;
privatestaticintCCSID;
privatestaticintport;
privatestaticvoidinit(){
//初始化服务器地址
//取得MQ消息发布服务器地址
Properties properties = ParserProperties.getSsoMQServer(null);
System.out.println("参数值==="+properties.toString());
hostname=properties.getProperty("hostname");//MQ服务器的IP地址
channel=properties.getProperty("channel");//服务器连接的通道
CCSID=Integer.parseInt(properties.getProperty("ccsid"));//服务器MQ服务使用的编码1381代表GBK、1208代表UTF(Coded CharacterSet Identifier:CCSID)
port=Integer.parseInt(properties.getProperty("port"));//MQ端口
qmName = properties.getProperty("qmname");//MQ的队列管理器名称
tName = properties.getProperty("tname");//topic 名称
try {
//if(tcf == null){
tcf = newMQTopicConnectionFactory();
tcf.setQueueManager(qmName);
tcf.setBrokerQueueManager(qmName);
tcf.setHostName(hostname);
tcf.setPort(port);
tcf.setChannel(channel);
tcf.setCCSID(CCSID);
tcf.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
tcf.setBrokerVersion(JMSC.MQJMS_BROKER_V1);
//tcf.setSubscriptionStore(JMSC.MQJMS_SUBSTORE_BROKER);//订阅存储
connection = (MQTopicConnection) tcf.createTopicConnection();
// connection.start();
session = (MQTopicSession) connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
//MQQueue queue = (MQQueue)session.createQueue("testQ");
topic = session.createTopic(tName);
subscriber = (MQTopicSubscriber) session.createSubscriber(topic);
MyListener listener = new MyListener();
subscriber.setMessageListener(listener);
//}
}catch (Exception e){
System.out.println("初始化MQ连接出错...");
Logger.error("初始化MQ连接出错 : "+e.getMessage());
e.printStackTrace();
}
}
publicstaticvoiddisconnect() throwsException {
subscriber.close();
session.close();
connection.stop();
connection.close();
tcf.clear();
System.out.println("Subscriber stopped.");
}
//接收消息初始化方法
publicstaticvoidinitReceiveMsg() throwsException{
init();
connection.start();
}
//接收消息
publicstaticString getRecevieMsg() throws Exception{
MyListener listener = (MyListener) subscriber.getMessageListener();
String msg = listener.getMsg() ;
while(msg == null){
Thread.sleep(10000);
msg = listener.getMsg() ;
break;
}
return listener.getMsg();
}
//发送消息
publicstaticvoidsengMsg(String msg) throws Exception{
init();
MQTopicSession pubSession =(MQTopicSession) connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
MQTopicPublisher publisher =(MQTopicPublisher) pubSession.createPublisher(topic);
TextMessage message =pubSession.createTextMessage();
message.setText(receiveMsgQ0002());
publisher.publish(message);
}
publicstaticString receiveMsgQ0002(){
String str = "<transaction>\n" +
"\t<header>\n" +
"\t\t<version>1.0</version>\n" +
"\t\t<message>\n" +
"\t\t\t<msgType>SSOS.000Q00020.01</msgType>\n" +
"\t\t\t<msgId>SSOS01222145148521022237</msgId>\n" +
"\t\t\t<sndDt>20151222</sndDt>\n" +
"\t\t\t<sndTm>145148521</sndTm>\n" +
"\t\t\t<callTyp>SYN</callTyp>\n" +
"\t\t\t<appCd>OSBS</appCd>\n" +
"\t\t\t<reqAppCd>SSOS</reqAppCd>\n" +
"\t\t\t<reserve>预留</reserve>\n"+
"\t\t</message>\n"+
"\t\t<security>\n"+
"\t\t\t<loginUser>SSOSTLOGIN</loginUser>\n" +
"\t\t\t<requestIp>192.168.1.211</requestIp>\n" +
"\t\t</security>\n"+
"\t\t<signature>355842a5-9f96-4c46-88fa-f9884a912574</signature>\n" +
"\t</header>\n" +
"\t<body>\n" +
"\t\t<request>\n" +
"\t\t\t<vocationalHeader>\n"+
"\t\t\t\t<serviceCode>Q0002</serviceCode>\n" +
"\t\t\t\t<appInfos>\n"+
"\t\t\t\t\t<appInfo>\n"+
"\t\t\t\t\t\t<appCode>ICCS</appCode>\n" +
"\t\t\t\t\t</appInfo>\n"+
"\t\t\t\t\t<appInfo>\n"+
"\t\t\t\t\t\t<appCode>FINM</appCode>\n" +
"\t\t\t\t\t</appInfo>\n"+
"\t\t\t\t</appInfos>\n"+
"\t\t\t\t<tfOrigChannel>CTS</tfOrigChannel>\n" +
"\t\t\t\t<menuCode/>\n"+
"\t\t\t\t<tranName/>\n"+
"\t\t\t\t<origExternalTask/>\n"+
"\t\t\t\t<tfChannel>SSOS</tfChannel>\n" +
"\t\t\t\t<externalTask/>\n"+
"\t\t\t\t<externalBranchCode/>\n"+
"\t\t\t\t<userId/>\n" +
"\t\t\t\t<checker/>\n"+
"\t\t\t\t<authorizer/>\n"+
"\t\t\t\t<authCode/>\n"+
"\t\t\t\t<revwCode/>\n"+
"\t\t\t\t<postingDateText>20151222</postingDateText>\n" +
"\t\t\t\t<xipDate/>\n"+
"\t\t\t\t<userReferenceNumber/>\n"+
"\t\t\t</vocationalHeader>\n"+
"\t\t\t<xfaceTradeDTO>\n"+
"\t\t\t\t<xfaceTFBroadcastAppInfoRegistryRequestDTO>\n" +
"\t\t\t\t\t<telInfos>\n"+
"\t\t\t\t\t\t<telInfo>\n"+
"\t\t\t\t\t\t\t<workDate>20130817</workDate>\n" +
"\t\t\t\t\t\t\t<telAddress></telAddress>\n" +
"\t\t\t\t\t\t\t<telCode>200004</telCode>\n" +
"\t\t\t\t\t\t\t<telSex>F</telSex>\n" +
"\t\t\t\t\t\t\t<telEmail/>\n"+
"\t\t\t\t\t\t\t<employmentType>1</employmentType>\n" +
"\t\t\t\t\t\t\t<branCode>2990001</branCode>\n" +
"\t\t\t\t\t\t\t<telBirth>19910827</telBirth>\n" +
"\t\t\t\t\t\t\t<legalCode>2990000</legalCode>\n" +
"\t\t\t\t\t\t\t<telIdcard>382983199108272987</telIdcard>\n" +
"\t\t\t\t\t\t\t<telMobile>13567587798</telMobile>\n" +
"\t\t\t\t\t\t\t<politicsStatus>1</politicsStatus>\n" +
"\t\t\t\t\t\t\t<telName>宋秋波</telName>\n"+
"\t\t\t\t\t\t\t<telJob>科员</telJob>\n"+
"\t\t\t\t\t\t\t<telSts>2</telSts>\n" +
"\t\t\t\t\t\t\t<telTell>0851-87650987</telTell>\n" +
"\t\t\t\t\t\t</telInfo>\n"+
"\t\t\t\t\t</telInfos>\n"+
"\t\t\t\t</xfaceTFBroadcastAppInfoRegistryRequestDTO>\n" +
"\t\t\t</xfaceTradeDTO>\n"+
"\t\t</request>\n"+
"\t</body>\n" +
"</transaction>\n";
return str;
}
publicstaticString receiveMsgQ0003(){
String str = "<transaction>\n" +
"\t<header>\n" +
"\t\t<version>1.0</version>\n" +
"\t\t<message>\n" +
"\t\t\t<msgType>SSOS.000Q00030.01</msgType>\n" +
"\t\t\t<msgId>SSOS01224153014318703150</msgId>\n" +
"\t\t\t<sndDt>20151224</sndDt>\n" +
"\t\t\t<sndTm>153014318</sndTm>\n" +
"\t\t\t<callTyp>SYN</callTyp>\n" +
"\t\t\t<appCd>OSBS</appCd>\n" +
"\t\t\t<reqAppCd>SSOS</reqAppCd>\n" +
"\t\t\t<reserve>预留</reserve>\n"+
"\t\t</message>\n"+
"\t\t<security>\n"+
"\t\t\t<loginUser>SSOSTLOGIN</loginUser>\n" +
"\t\t\t<requestIp>127.0.0.1</requestIp>\n" +
"\t\t</security>\n"+
"\t\t<signature>d07cfcd6-7ae1-4fff-ac47-eafa92f52f90</signature>\n" +
"\t</header>\n" +
"\t<body>\n" +
"\t\t<request>\n" +
"\t\t\t<vocationalHeader>\n" +
"\t\t\t\t<serviceCode>Q0003</serviceCode>\n" +
"\t\t\t\t<appInfos>\n"+
"\t\t\t\t\t<appInfo>\n"+
"\t\t\t\t\t\t<appCode>EAST</appCode>\n" +
"\t\t\t\t\t</appInfo>\n"+
"\t\t\t\t</appInfos>\n"+
"\t\t\t\t<tfOrigChannel>CTS</tfOrigChannel>\n" +
"\t\t\t\t<menuCode/>\n"+
"\t\t\t\t<tranName/>\n"+
"\t\t\t\t<origExternalTask/>\n"+
"\t\t\t\t<tfChannel>SSOS</tfChannel>\n" +
"\t\t\t\t<externalTask/>\n"+
"\t\t\t\t<externalBranchCode/>\n"+
"\t\t\t\t<userId/>\n"+
"\t\t\t\t<checker/>\n"+
"\t\t\t\t<authorizer/>\n"+
"\t\t\t\t<authCode/>\n"+
"\t\t\t\t<revwCode/>\n"+
"\t\t\t\t<postingDateText>20151224</postingDateText>\n" +
"\t\t\t\t<xipDate/>\n"+
"\t\t\t\t<userReferenceNumber/>\n" +
"\t\t\t</vocationalHeader>\n"+
"\t\t\t<xfaceTradeDTO>\n"+
"\t\t\t\t<xfaceTFBroadcastAppInfoCancelRequestDTO>\n" +
"\t\t\t\t\t<telCode>200004</telCode>\n" +
"\t\t\t\t</xfaceTFBroadcastAppInfoCancelRequestDTO>\n" +
"\t\t\t</xfaceTradeDTO>\n"+
"\t\t</request>\n"+
"\t</body>\n" +
"</transaction>";
return str;
}
}
4、 消息接收监听类
packagesso.gznx.timer;
importjava.io.ByteArrayOutputStream;
importjavax.jms.Message;
importjavax.jms.MessageListener;
importnc.bs.logging.Logger;
importcom.ibm.jms.JMSBytesMessage;
importcom.ibm.jms.JMSTextMessage;
/**
* 消息订阅者监听
*@author lijbe
*
*/
public class MyListener implements MessageListener{
privateString msg = null;
public void onMessage(Message m) {
try {
if(m instanceof JMSBytesMessage){
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byte [] buffer = new byte[256];
int length = 0;
JMSBytesMessage byteMsg = (JMSBytesMessage)m;
while((length = byteMsg.readBytes(buffer) )!=-1){
byteStream.write(buffer,0,length);
}
msg = new String(byteStream.toByteArray(),"utf-8");
}if(m instanceof JMSTextMessage){
msg = ((JMSTextMessage)m).getText();
}
} catch (Exception e) {
Logger.error(e.getMessage());
e.printStackTrace();
}
}
public String getMsg() {
return msg;
}
}
5、自我测试,一般商业使用消息管理服务器会使用IBMMQ,如果是自己学习或者搭建的话,也可以使用ActiveMQ来作为免费的服务器。
6、消息订阅需要设置一个定时任务去扫描消息服务器有没有发布消息。