WEB 項目中JMS的簡單應用

來源:互聯網
上載者:User

標籤:cto   exce   des   隊列   cat   nbsp   vax   instance   應用   

1.現在pom.xml 檔案中引入所需的依賴

2. 配置application.xml

<!-- Activemq 串連工廠 -->
<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg value="admin" />
<constructor-arg value="admin" />
<constructor-arg value="failover:(tcp://192.168.100.109:61616)?timeout=2000" />
</bean>

<!-- ConnectionFactory Definition -->
<bean id="amqConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="activeMQConnectionFactory" />
</bean>

<!-- Default Destination Queue Definition -->
<!-- 測試組態多個Destination -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="MC_MESSAGE" />
</bean>

<!-- JmsTemplate Definition -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="amqConnectionFactory" />
<property name="defaultDestination" ref="destination" />
</bean>

<!-- Message Sender Definition -->
<bean id="messageSender" class="com.xxx.config.MessageSender">
<constructor-arg index="0" ref="jmsTemplate" />
<constructor-arg index="1" ref="destination" />
</bean>
<!-- 訊息監聽器,主要監聽的目的地址 Message Receiver Definition -->
<bean id="messageReceiver" class="com.xxx.config.MessageReceiver">
</bean>
<bean class="org.springframework.jms.listener.SimpleMessageListenerContainer">
<property name="connectionFactory" ref="amqConnectionFactory" />
<property name="destinationName" value="MC_MESSAGE" />
<property name="messageListener" ref="messageReceiver" />
</bean>

3. 書寫MessageSender 

package com.xxx.config;

import javax.jms.Destination;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

@Component
public class MessageSender {
private final JmsTemplate jmsTemplate;
private final Destination destination;

public MessageSender(final JmsTemplate jmsTemplate, final Destination destination) {
this.jmsTemplate = jmsTemplate;
this.destination = destination;
}

public void send(final String text) {
try {
jmsTemplate.setDefaultDestination(destination);
jmsTemplate.convertAndSend(text);
// System.out.println("發送訊息 : " + text);
} catch (Exception e) {
e.printStackTrace();
}
}
}

4.訊息MessageReceiver 

package com.xxx.config;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class MessageReceiver implements MessageListener {

public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
textMessage.getText();
// System.out.println("接收到訊息: " + text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}

}

 5介面.調用 即可將訊息發送到指定的訊息佇列當中

@Resource(name="messageSender")
private MessageSender messageSender;

//定義訊息實體,儲存內容,並將實體轉化為字串  調用send方法 將訊息發送出去,同時訊息監聽會列印監聽到的訊息

QueneVo Vo = new QueneVo();
Vo.setId(problem.getProblemId());
Vo.setTitle("建立了新問題【"+problem.getProblemId()+"】,需要您處理");
Vo.setContent(problem.getProblemDescribe());
Vo.setMsgTime(DateUtil.getTime(0));
this.messageSender.send(JsonUtils.writeObject(Vo));

 

WEB 項目中JMS的簡單應用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.