您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
<!--Spring配置部分--> <!--创建连接工厂 --> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.1.13:61616"></property> </bean> <!-- 声明ActiveMQ消息目标,目标可以是一个队列,也可以是一个主题ActiveMQTopic --> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg index="0" value="TransferIno"></constructor-arg> </bean> <!--定义模板---> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"></property> <property name="defaultDestination" ref="destination"></property> <property name="receiveTimeout" value="600"></property> </bean> <!--发送--> <bean id="sender" class="com.xbkj.springactivemq2.Sender"> <property name="jmsTemplate" ref="jmsTemplate"></property> </bean> <!--接受--> <bean id="receiver" class="com.xbkj.springactivemq2.Receiver"> <property name="jmsTemplate" ref="jmsTemplate"></property> </bean> //JAVA部分 package com.xbkj.springactivemq2; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ScheduledMessage; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; //发送 public class Sender { private JmsTemplate jmsTemplate; private String info; public JmsTemplate getJmsTemplate() { return jmsTemplate; } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public void sendInfo() { System.out.println("info:"+info); if (info != null && !"".equals(info)){ jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session)throws JMSException { TextMessage message = session.createTextMessage(info); long delayTime = 3000; message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delayTime); return message; } }); } } public void setInfo(String info) { this.info = info; } public String getInfo() { return info; } } package com.xbkj.springactivemq2; import javax.jms.JMSException; import javax.jms.TextMessage; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.support.JmsUtils; //接收 public class Receiver { private JmsTemplate jmsTemplate; public JmsTemplate getJmsTemplate() { return jmsTemplate; } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } /** * 构造函数 */ public Receiver3() { } public String receiveMessage() { String my = ""; TextMessage message = (TextMessage) jmsTemplate.receive(); try { my = message.getText(); } catch (JMSException e) { throw JmsUtils.convertJmsAccessException(e); } return my; } } //单元测试 package com.xbkj.service.test; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xbkj.springactivemq2.Receiver3; import com.xbkj.springactivemq2.Sender3; public class MessageTest { private ApplicationContext context; @Before public void init() { context = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testMessageSender(){ Sender3 sender3 =(Sender3)context.getBean("sender3"); int state =4; if(state ==4){ sender3.setInfo("test1"); sender3.sendInfo(); } Receiver3 receiver3= (Receiver3) context.getBean("receiver3"); System.out.println(receiver3.receiveMessage()); } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。