IBM MQ刚接触,今天就SPRING整合写了个DEMO,借助于《SPRING攻略》
spring xml 的配置:
文件名:applicationContext-biz-mq.xml
MQ
classpath:/mqconnect.properties
mqconnect.properties配置文件:
#通道名
app.mq.channel=SYSTEM.DEF.SVRCONN
#传输类型
app.mq.transportType=1
#端口号
app.mq.port=1414
#队列管理器名称-发送方
queue.manager.send=WMQ1QM
#主机地址-发送方
queue.manager.host.send=10.70.175.81
#队列名称-发送方
queue.name.send=WMQ1OutputQ
#队列管理器名称--接收方
queue.manager.get=WMQ2QM
#主机地址--接收方
queue.manager.host.get=10.70.175.82
#队列名称--接收方
queue.name.get=WMQ2InputQ
MessageTest.java测试文件,用来启动接收监听的配置文件:
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MessageTest {
/**
* @param args
*/
public static void main(String[] args) {
new ClassPathXmlApplicationContext("classpath*:/applicationContext-biz-mq.xml");
}
}
MessageListener.java监听文件:
import com.zmcc.servicemanager.biz.CallRecordBiz;
import com.zmcc.servicemanager.domain.CallRecord;
/**
* 消息监听器
* @author ZouXia
*
*/
public class MessageListener{
private CallRecordBiz callRecordBiz;
/**
* 接收消息
* @param callRecord
*/
public void receviedMessage(CallRecord callRecord) {
System.out.println(callRecord.getRequestContent());
// callRecordBiz.saveEntity(callRecord);
}
public CallRecordBiz getCallRecordBiz() {
return callRecordBiz;
}
public void setCallRecordBiz(CallRecordBiz callRecordBiz) {
this.callRecordBiz = callRecordBiz;
}
}
MessageConverter.java用来转换的类:
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.jms.support.converter.MessageConversionException;
import com.zmcc.servicemanager.domain.CallRecord;
/**
* 消息转换器
* @author ZouXia
*
*/
public class MessageConverter implements org.springframework.jms.support.converter.MessageConverter {
/**
* 发送消息的转换
*/
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
return null;
}
/**
* 接收消息的转换
*/
public Object fromMessage(Message message) throws JMSException,MessageConversionException {
// TODO
TextMessage mapMessage = (TextMessage) message;
CallRecord callRecord = new CallRecord();
callRecord.setEndTime(new Date());
callRecord.setRequestContent(mapMessage.getText());
return callRecord;
}
}
CallRecord.java实体类
public class CallRecord implements Serializable{
private static final long serialVersionUID = 1L;
private String requestContent;
private Date endTime;
……
}
JmsProducer.java发送消息:
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import com.ibm.mq.jms.JMSC;
import com.ibm.mq.jms.MQQueueConnectionFactory;
/**
* 点对点模式
* @author ZouXia
*
*/
public class JmsProducer {
/**
* Main method
*
* @param args
*/
public static void main(String[] args) {
// Variables
Connection connection = null;
Session session = null;
Destination destination = null;
MessageProducer producer = null;
try {
// Create a connection factory
// objects
MQQueueConnectionFactory factory = new MQQueueConnectionFactory();
factory.setQueueManager("WMQ1QM");
factory.setHostName("10.70.175.81");
factory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
factory.setPort(1414);
factory.setChannel("SYSTEM.DEF.SVRCONN");
// Create JMS objects
connection = factory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("WMQ1OutputQ");
producer = session.createProducer(destination);
long uniqueNumber = System.currentTimeMillis() % 1000;
TextMessage message = session
.createTextMessage("JmsProducer: Your lucky number today is " + uniqueNumber);
// Start the connection
connection.start();
// And, send the message
producer.send(message);
System.out.println("Sent message:\n" + message);
} catch (JMSException jmsex) {
jmsex.fillInStackTrace();
} finally {
try {
producer.close();
session.close();
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
所对应的发送方WMQ1QM的资源管理器:
所对应的通道:
接收方WMQ2QM的队列:
通道:
资源管理器即IBM MQ explorer是基于eclipse的进行的,在eclipse里面添加plug插件即可,其更新地址为:
http://public.dhe.ibm.com/software/integration/wmq/explorer/v75/updates/
Exception in thread "main" java.lang.NoClassDefFoundError: javax/resource/ResourceException
at com.ibm.mq.MQEnvironment.(MQEnvironment.java:490)
at com.ibm.mq.jms.services.ConfigEnvironment.(ConfigEnvironment.java:190)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at com.ibm.mq.jms.MQConnectionFactory$1.run(MQConnectionFactory.java:658)
at java.security.AccessController.doPrivileged(Native Method)
at com.ibm.mq.jms.MQConnectionFactory.(MQConnectionFactory.java:651)
at com.zmcc.servicemanager.mq.jms.JmsProducer.main(JmsProducer.java:36)
Caused by: java.lang.ClassNotFoundException: javax.resource.ResourceException
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 8 more
javax.resource
connector
1.0