您好,登錄后才能下訂單哦!
<!--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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。