一、dependency
為蒙自等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及蒙自網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、蒙自網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!<properties> <activemq.version>5.15.4</activemq.version> <xbean-spring.version>4.8</xbean-spring.version> <spring-jms.version>5.0.7.RELEASE</spring-jms.version> </properties> <dependencies> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> <version>${activemq.version}</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-spring</artifactId> <version>${activemq.version}</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>${activemq.version}</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> <version>${activemq.version}</version> </dependency> <dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>${xbean-spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring-jms.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> </dependency> </dependencies>
二、activemq.properties
active.config.brokerURL=failover:(tcp://localhost:61617,tcp://localhost:61618,tcp://localhost:61619) active.config.username=admin active.config.password=admin123 active.destination.queue.name=queue.test01 active.destination.topic.name=topic.test01
三、spring-activemq-producer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath*:activemq.properties" ignore-unresolvable="true" />
<!-- 定義ReDelivery(重發(fā)機(jī)制)機(jī)制 ,重發(fā)時(shí)間間隔是100毫秒,大重發(fā)次數(shù)是3次 http://www.kuqin.com/shuoit/20140419/339344.html -->
<bean id="activeMQRedeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy">
<!--是否在每次嘗試重新發(fā)送失敗后,增長(zhǎng)這個(gè)等待時(shí)間 -->
<property name="useExponentialBackOff" value="true"></property>
<!--重發(fā)次數(shù),默認(rèn)為6次 這里設(shè)置為1次 -->
<property name="maximumRedeliveries" value="1"></property>
<!--重發(fā)時(shí)間間隔,默認(rèn)為1秒 -->
<property name="initialRedeliveryDelay" value="1000"></property>
<!--第一次失敗后重新發(fā)送之前等待500毫秒,第二次失敗再等待500 * 2毫秒,這里的2就是value -->
<property name="backOffMultiplier" value="2"></property>
<!--大傳送延遲,只在useExponentialBackOff為true時(shí)有效(V5.5),假設(shè)首次重連間隔為10ms,倍數(shù)為2,那么第
二次重連時(shí)間間隔為 20ms,第三次重連時(shí)間間隔為40ms,當(dāng)重連時(shí)間間隔大的大重連時(shí)間間隔時(shí),以后每次重連時(shí)間間隔都為大重連時(shí)間間隔。 -->
<property name="maximumRedeliveryDelay" value="1000"></property>
</bean>
<!-- activemq連接工廠 -->
<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>${active.config.brokerURL}</value>
</property>
<property name="userName">
<value>${active.config.username}</value>
</property>
<property name="password">
<value>${active.config.password}</value>
</property>
<property name="redeliveryPolicy" ref="activeMQRedeliveryPolicy"/>
</bean>
<!-- 連接池 -->
<bean id="pooledJmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<constructor-arg name="activeMQConnectionFactory" ref="jmsFactory" />
</bean>
<!-- 消費(fèi)發(fā)送和接收模板 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg name="connectionFactory" ref="pooledJmsFactory" />
</bean>
<!-- destination:queue和topic -->
<bean id="queueDest" class="org.apache.activemq.command.ActiveMQQueue" autowire="constructor">
<constructor-arg value="${active.destination.queue.name}" />
</bean>
<bean id="topicDest" class="org.apache.activemq.command.ActiveMQTopic" autowire="constructor">
<constructor-arg value="${active.destination.topic.name}" />
</bean>
<!--業(yè)務(wù)自定義 queue-->
<bean id="queueProducer" class="com.demo.activemq.QueueActivemqProducer">
<constructor-arg name="jmsTemplate" ref="jmsTemplate" />
<constructor-arg name="destination" ref="queueDest" />
</bean>
<!--業(yè)務(wù)自定義 topic-->
<bean id="topicProducer" class="com.demo.activemq.TopicActivemqProducer">
<constructor-arg name="jmsTemplate" ref="jmsTemplate" />
<constructor-arg name="destination" ref="topicDest" />
</bean>
</beans>
四、spring-activemq-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath*:activemq.properties" ignore-unresolvable="true" />
<!-- activemq連接工廠 -->
<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>${active.config.brokerURL}</value>
</property>
<property name="userName">
<value>${active.config.username}</value>
</property>
<property name="password">
<value>${active.config.password}</value>
</property>
</bean>
<!-- 連接池 -->
<bean id="pooledJmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<constructor-arg name="activeMQConnectionFactory" ref="jmsFactory" />
</bean>
<!-- 消費(fèi)發(fā)送和接收模板 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg name="connectionFactory" ref="pooledJmsFactory" />
</bean>
<!-- destination:queue和topic -->
<bean id="queueDest" class="org.apache.activemq.command.ActiveMQQueue" autowire="constructor">
<constructor-arg value="${active.destination.queue.name}" />
</bean>
<bean id="topicDest" class="org.apache.activemq.command.ActiveMQTopic" autowire="constructor">
<constructor-arg value="${active.destination.topic.name}" />
</bean>
<!-- 業(yè)務(wù)處理器 -->
<bean id="QueueHandler" class="com.demo.activemq.QueueHandler" />
<!-- 業(yè)務(wù)實(shí)現(xiàn)的監(jiān)聽器 -->
<bean id="msgListener" class="com.demo.activemq.CustomerMsgListener">
<constructor-arg name="businessHandler" ref="QueueHandler"/>
</bean>
<!-- 消費(fèi)者整合監(jiān)聽器 -->
<bean id="queueConsumer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory" />
<property name="destination" ref="queueDest" />
<property name="messageListener" ref="msgListener" />
</bean>
<!-- 消費(fèi)者整合監(jiān)聽器 -->
<bean id="topicConsumer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory" />
<property name="destination" ref="topicDest" />
<property name="messageListener" ref="msgListener" />
</bean>
</beans>
五、相關(guān)業(yè)務(wù)實(shí)現(xiàn)類
producer相關(guān)類:
import org.springframework.jms.core.JmsTemplate;
import javax.jms.Destination;
public abstract class AbstractActivemqProducer {
private JmsTemplate jmsTemplate;
private Destination destination;
public AbstractActivemqProducer(JmsTemplate jmsTemplate, Destination destination) {
this.jmsTemplate = jmsTemplate;
this.destination = destination;
}
public void send(String msg){
jmsTemplate.convertAndSend(destination, msg);
}
}
public class QueueActivemqProducer extends AbstractActivemqProducer {
public QueueActivemqProducer(JmsTemplate jmsTemplate, Destination destination) {
super(jmsTemplate, destination);
}
}
public class TopicActivemqProducer extends AbstractActivemqProducer {
public TopicActivemqProducer(JmsTemplate jmsTemplate, Destination destination) {
super(jmsTemplate, destination);
}
}
customer相關(guān)類:
public class CustomerMsgListener implements MessageListener { private BusinessHandler businessHandler; public CustomerMsgListener(BusinessHandler businessHandler) { this.businessHandler = businessHandler; } @Override public void onMessage(Message message) { try { if (message instanceof TextMessage) { businessHandler.handle(((TextMessage) message).getText() ); } if (message instanceof MapMessage) { MapMessage mapMessage = (MapMessage) message; businessHandler.handle(mapMessage.getString("key01") ); businessHandler.handle(mapMessage.getString("key02") ); } } catch (JMSException e) { e.printStackTrace(); } } } public interface BusinessHandler { void handle(String msg); } public class QueueHandler implements BusinessHandler { @Override public void handle(String msg) { System.out.println("msg = [" + msg + "]"); } }
六、測(cè)試
public class XmlActivemqTest { public static void main(String[] args) { customerXml(); } public static void producerXml(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring-activemq-producer.xml"); AbstractActivemqProducer queueActivemqProducer = context.getBean(QueueActivemqProducer.class); queueActivemqProducer.send("this is a test"); } public static void customerXml(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:spring-activemq-customer.xml"); } }
參考地址:
http://activemq.apache.org/spring-support.html
http://docs.spring.io/spring/docs/2.5.x/reference/jms.html#jms-mdp
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前文章:activemqspring客戶端-創(chuàng)新互聯(lián)
文章URL:http://jinyejixie.com/article14/pihge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、標(biāo)簽優(yōu)化、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站排名、外貿(mào)建站、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容