WebSphere MQ简单实例(发送消息)
程序员文章站
2022-07-13 12:17:52
...
MQ消息发送:
package com.main;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
/**
* MQ发送消息
*
* @author
*/
public class TestSend
{
/**
* jms模板,封装链接工厂、队列、消息生产者
*/
private JmsTemplate jmsTemplate;
public TestSend()
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
jmsTemplate = (JmsTemplate)ctx.getBean("jmsTemplate");
}
/**
* 发送消息
*
* @param msg消息
*/
public void showResult(String msg)
{
// Text消息
MessageCreator messageCreator = createTxtMsg(msg);
jmsTemplate.send(messageCreator);
// Map消息
messageCreator = createMapMsg(msg);
jmsTemplate.send(messageCreator);
}
/**
* 创建text消息
*
* @param msg 消息
* @return message
*/
private MessageCreator createTxtMsg(final String msg)
{
return new MessageCreator()
{
public Message createMessage(Session session) throws JMSException
{
TextMessage textMessage = session.createTextMessage();
textMessage.setText(msg);
return textMessage;
}
};
}
/**
* 创建map消息
*
* @param msg消息
* @return message
*/
private MessageCreator createMapMsg(final String msg)
{
return new MessageCreator()
{
public Message createMessage(Session session) throws JMSException
{
MapMessage mapMessage = session.createMapMessage();
mapMessage.setString("map", "javax.jms.MapMessage");
mapMessage.setString("map1", msg);
return mapMessage;
}
};
}
/**
* @param args
*/
public static void main(String[] args)
{
TestSend send = new TestSend();
send.showResult("start, send!");
}
}
配置文件:
<bean id="jmsConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="hostName" value="10.21.139.43" />
<property name="port" value="1414" />
<property name="CCSID" value="1381" />
<property name="queueManager" value="QM_SN_CNHQ_9379C" />
</bean>
<bean id="queue" class="com.ibm.mq.jms.MQQueue">
<property name="baseQueueName" value="default" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="defaultDestination" ref="queue" />
<property name="pubSubDomain" value="false" />
</bean>
package com.main;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
/**
* MQ发送消息
*
* @author
*/
public class TestSend
{
/**
* jms模板,封装链接工厂、队列、消息生产者
*/
private JmsTemplate jmsTemplate;
public TestSend()
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
jmsTemplate = (JmsTemplate)ctx.getBean("jmsTemplate");
}
/**
* 发送消息
*
* @param msg消息
*/
public void showResult(String msg)
{
// Text消息
MessageCreator messageCreator = createTxtMsg(msg);
jmsTemplate.send(messageCreator);
// Map消息
messageCreator = createMapMsg(msg);
jmsTemplate.send(messageCreator);
}
/**
* 创建text消息
*
* @param msg 消息
* @return message
*/
private MessageCreator createTxtMsg(final String msg)
{
return new MessageCreator()
{
public Message createMessage(Session session) throws JMSException
{
TextMessage textMessage = session.createTextMessage();
textMessage.setText(msg);
return textMessage;
}
};
}
/**
* 创建map消息
*
* @param msg消息
* @return message
*/
private MessageCreator createMapMsg(final String msg)
{
return new MessageCreator()
{
public Message createMessage(Session session) throws JMSException
{
MapMessage mapMessage = session.createMapMessage();
mapMessage.setString("map", "javax.jms.MapMessage");
mapMessage.setString("map1", msg);
return mapMessage;
}
};
}
/**
* @param args
*/
public static void main(String[] args)
{
TestSend send = new TestSend();
send.showResult("start, send!");
}
}
配置文件:
<bean id="jmsConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="hostName" value="10.21.139.43" />
<property name="port" value="1414" />
<property name="CCSID" value="1381" />
<property name="queueManager" value="QM_SN_CNHQ_9379C" />
</bean>
<bean id="queue" class="com.ibm.mq.jms.MQQueue">
<property name="baseQueueName" value="default" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="defaultDestination" ref="queue" />
<property name="pubSubDomain" value="false" />
</bean>
推荐阅读
-
python 发送和接收ActiveMQ消息的实例
-
Android Intent发送广播消息实例详解
-
WebSphere MQ简单实例(发送消息)
-
WebSphere MQ简单实例(接收消息)
-
ROS学习笔记(3)之ros的topic发送与接收简单实例
-
python实现企业微信定时发送文本消息的实例代码
-
手机注册发送验证码倒计时的简单实例
-
C# Queue与RabbitMQ的爱恨情仇(文末附源码):Q与MQ消息队列简单应用(一)
-
使用spring stream发送消息代码实例
-
Channel closed; cannot ack/nack 和 MQ的重复消费/重复发送消息问题(记RabbitMQ使用的几个小坑)