ActiveMQ 6.Spring整合ActiveMQ

ActiveMQ

@Author:hanguixian

@Email:[email protected]

六 Spring整合ActiveMQ

1 代码

  • 项目结构
    ActiveMQ 6.Spring整合ActiveMQ_第1张图片

  • pom.xml 依赖

<dependencies>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>5.2.0.RELEASEversion>
    dependency>

    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jmsartifactId>
      <version>5.2.0.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>org.apache.activemqgroupId>
      <artifactId>activemq-poolartifactId>
      <version>5.15.10version>
    dependency>
    
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>2.10.0version>
    dependency>

    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>

    <dependency>
      <groupId>org.slf4jgroupId>
      <artifactId>slf4j-simpleartifactId>
      <version>1.7.25version>
    dependency>

  dependencies>
  • spring配置:applicationContext.xml

<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/context
http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <context:component-scan base-package="com.hgx.spring.activemq"/>

      
    <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
        <property name="connectionFactory">
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="tcp://106.14.217.80:61616"/>
            bean>
        property>
        <property name="maxConnections" value="100"/>
    bean>

    
    <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="spring-active-queue"/>
    bean>

    
    <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg index="0" value="spring-active-topic"/>
    bean>

 
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        
        <property name="connectionFactory" ref="jmsFactory"/>
         
        <property name="defaultDestination" ref="destinationQueue"/>
         
        <property name="messageConverter">
            <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
        property>
    bean>

     
    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="jmsFactory"/>
        <property name="destination" ref="destinationQueue"/>
        <property name="messageListener" ref="myMessageListener"/>
    bean>

beans>
  • 消息生产者:ActiveMQProduce.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

@Component
public class ActiveMQProduce {

    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        ActiveMQProduce activeMQProduce = (ActiveMQProduce) applicationContext.getBean("activeMQProduce");
        activeMQProduce.jmsTemplate.send(session -> session.createTextMessage("-------spring activeMQ message--------"));
        System.out.println("--------spring message send success-----------");
    }
}
  • 消息消费者–方式一:手动消费 只需要使用jmsTemplate.receiveAndConvert() 即可
//消费
String content = (String)jmsTemplate.receiveAndConvert();
  • 消息消费者–方式二:监听消费,实现MessageListener接口,重写onMessage方法,在sping配置中配置监听,如上面applicationContext.xml配置所示(jmsContainer)
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@Component
public class MyMessageListener implements MessageListener {

    @Override
    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) message;
            try {
                System.out.println("消费:" + textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(ActiveMQ,消息中间件)