91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

activemq spring客戶端

發布時間:2020-05-18 03:11:38 來源:網絡 閱讀:361 作者:乾坤刀 欄目:軟件技術

一、dependency

<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(重發機制)機制 ,重發時間間隔是100毫秒,最大重發次數是3次 http://www.kuqin.com/shuoit/20140419/339344.html -->
    <bean id="activeMQRedeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy">
        <!--是否在每次嘗試重新發送失敗后,增長這個等待時間 -->
        <property name="useExponentialBackOff" value="true"></property>
        <!--重發次數,默認為6次   這里設置為1次 -->
        <property name="maximumRedeliveries" value="1"></property>
        <!--重發時間間隔,默認為1秒 -->
        <property name="initialRedeliveryDelay" value="1000"></property>
        <!--第一次失敗后重新發送之前等待500毫秒,第二次失敗再等待500 * 2毫秒,這里的2就是value -->
        <property name="backOffMultiplier" value="2"></property>
        <!--最大傳送延遲,只在useExponentialBackOff為true時有效(V5.5),假設首次重連間隔為10ms,倍數為2,那么第
       二次重連時間間隔為 20ms,第三次重連時間間隔為40ms,當重連時間間隔大的最大重連時間間隔時,以后每次重連時間間隔都為最大重連時間間隔。 -->
        <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>

   <!-- 消費發送和接收模板 -->
   <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>

   <!--業務自定義 queue-->
   <bean id="queueProducer" class="com.demo.activemq.QueueActivemqProducer">
       <constructor-arg name="jmsTemplate" ref="jmsTemplate" />
       <constructor-arg name="destination" ref="queueDest" />
   </bean>

   <!--業務自定義 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>

   <!-- 消費發送和接收模板 -->
   <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>

   <!-- 業務處理器 -->
   <bean id="QueueHandler" class="com.demo.activemq.QueueHandler" />
   <!-- 業務實現的監聽器 -->
   <bean id="msgListener" class="com.demo.activemq.CustomerMsgListener">
       <constructor-arg name="businessHandler" ref="QueueHandler"/>
   </bean>

   <!-- 消費者整合監聽器 -->
   <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>

   <!-- 消費者整合監聽器 -->
   <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>


五、相關業務實現類

producer相關類:

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相關類:

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 + "]");
    }

}

六、測試

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


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

平罗县| 清水河县| 富宁县| 旬阳县| 扶余县| 赤水市| 济源市| 陈巴尔虎旗| 石嘴山市| 惠水县| 勃利县| 淳化县| 大悟县| 那曲县| 合作市| 深泽县| 大庆市| 轮台县| 健康| 诏安县| 格尔木市| 依安县| 玉门市| 禹城市| 兴国县| 内黄县| 凌云县| 武义县| 绥化市| 镇宁| 焉耆| 容城县| 和田县| 琼结县| 信阳市| 龙泉市| 蒲城县| 兰考县| 夏河县| 萝北县| 汉川市|