activemqspring客户端-创新互联

一、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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

当前文章:activemqspring客户端-创新互联
文章URL:https://www.cdcxhl.com/article14/pihge.html

成都网站建设公司_创新互联,为您提供企业建站标签优化网页设计公司网站排名外贸建站网站营销

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

微信小程序开发