spring整合activemq

一.avtiveMQ的控制台【activemq连接解决方案】

1.设置activemq的浏览器访问地址

在apache-activemq-5.16.0/conf文件夹下的jetty.xml文件编译下面代码块。

(1)代码块

<!-- 在apache-activemq-5.16.0/conf文件夹下的jetty.xml文件编译下面代码块 -->
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
             <!-- 设置ip地址 -->
        <property name="host" value="192.168.8.88"/>
        <!-- 设置访问端口 -->
        <property name="port" value="8161"/>
    </bean>

(2)范例

图片

图片

2.设置linux的防火墙关闭和window关闭防火墙的指令

(1)linux的防火墙

# 开启防火墙
systemctl start firewalld
# 关闭防火墙
systemctl stop firewalld
# 查看防火墙状态
systemctl status firewalld

(2)window的防火墙

# 开启防火墙
netsh advfirewall set allprofiles state on
# 关闭防火墙
netsh advfirewall set allprofiles state off
# 查看防火墙状态
Netsh Advfirewall show allprofiles

(3)测试ping的连接

图片

图片

4.访问activeMQ的控制台

(1)访问地址

# 访问地址
http://192.168.8.88:8161/
# 默认登录的用户名和密码
账号:admin
密码:admin

(2)范例

图片

图片

二.连接端口的总结

采用61616端口提供JMS服务。

采用8161端口提供管理控制台服务。

三.activemq的队列整合spring

1.pom.xml

(1)代码块

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>spring-activemq</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>spring-activemq Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <dependencies>
    <!-- activemq所需要的jar包 -->
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-all</artifactId>
      <version>5.15.9</version>
    </dependency>
    <dependency>
      <groupId>org.apache.xbean</groupId>
      <artifactId>xbean-spring</artifactId>
      <version>3.16</version>
    </dependency>
    <!-- activemq的broker服务器 -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.5</version>
    </dependency>
    <!-- activemq对JMS的支持,整合springle和activemq -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
      <version>4.3.23.RELEASE</version>
    </dependency>
    <!-- activemq所需要的pool包配置 -->
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-pool</artifactId>
      <version>5.15.9</version>
    </dependency>
    <!-- spring-aop等相关jar -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.23.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.23.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.23.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>4.3.23.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.5.3</version>
    </dependency>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>2.1_2</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>spring-activemq</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

(2)范例

图片

图片

图片

图片

图片

2.applicationContext配置文件

(1)代码块

<?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:component-scan base-package="com.fengmo.activemq"/>
    <!-- 配置生产者 -->
    <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
        <property name="connectionFactory">
            <!-- 真正可以产生Connection的ConnectionFactory,由对应的JMS服务厂商提供 -->
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="tcp://192.168.83.130:61616"/>
            </bean>
        </property>
        <property name="maxConnections" value="100"></property>
    </bean>
    <!-- 这个是队列目的地,点对点的。注意:该bean多个配置就可以根据需求切换目的地 -->
    <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="spring-active-queue"/>
    </bean>
    <!-- Spring提供的JMS工具类,它可以进行消息发送、接受等【队列】 -->
    <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>
</beans>

(2)范例

图片

图片

2.消息生产者

(1)代码块

package com.fengmo.activemq.queue;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.jms.TextMessage;
@Service
/**
 * 队列的生产者
 */
public class SpringMQ_Produce {
    @Autowired
    private JmsTemplate jmsTemplate;    //需要在配置文件配置bean对象
    public static void main(String[] args) {
        //1.加载依赖
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.获取该类的bean对象,注意参数第一个字母小写
        SpringMQ_Produce produce =(SpringMQ_Produce)applicationContext.getBean("springMQ_Produce");
        //3.获取jmsTemplate对象发送消息
        //3.1 原版写法
//        produce.jmsTemplate.send(new MessageCreator() {    
//            @Override
//            public Message createMessage(Session session) throws JMSException {
//                //创建消息
//                TextMessage message = session.createTextMessage("发送一个字符串类型的消息");
//                return message;
//            }
//        });
        //3.2 lambda表达式
        produce.jmsTemplate.send((session -> {
            //创建消息
            TextMessage message = session.createTextMessage("spring和activemq的整合");
            return message;
        }));
        System.out.println("*****消息发送完成****");
    }
}

(2)范例

图片

图片

3.消息消费者

(1)代码块

package com.fengmo.activemq;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.jms.TextMessage;
/**
 * 队列的消费者
 */
@Service
public class SpringMQ_Consumer {
    @Autowired
    private JmsTemplate jmsTemplate;
    public static void main(String[] args) {
        //1.加载依赖
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.获取该类的bean对象,注意参数第一个字母小写
        SpringMQ_Consumer consumer =(SpringMQ_Consumer)applicationContext.getBean("springMQ_Consumer");
        //3.获取jmsTemplate对象获取消息
        String message = (String) consumer.jmsTemplate.receiveAndConvert();
        System.out.println("这是接受的字符串" + message);
    }
}

(2)范例

图片

4.注意事项

操作的消费类和生产类都是同步操作,而非异步操作。如果异步操作,请看监听配置。

四.activemq的主题整合spring

1.pom.xml

参考activemq的队列整合spring。

2.applicationContext.xml配置文件

(1)代码块

<?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:component-scan base-package="com.fengmo.activemq"/>
    <!-- 配置生产者 -->
    <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
        <property name="connectionFactory">
            <!-- 真正可以产生Connection的ConnectionFactory,由对应的JMS服务厂商提供 -->
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="tcp://192.168.83.130:61616"/>
            </bean>
        </property>
        <property name="maxConnections" value="100"></property>
    </bean>
    <!-- 这个是队列目的地,点对点的。注意:该bean多个配置就可以根据需求切换目的地 -->
<!--    <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">-->
<!--        <constructor-arg index="0" value="spring-active-queue"/>-->
<!--    </bean>-->
    <!-- Spring提供的JMS工具类,它可以进行消息发送、接受等【队列】 -->
<!--    <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="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg index="0" value="spring-active-topic"/>
    </bean>
    <!-- Spring提供的JMS工具类,它可以进行消息发送、接受等 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsFactory"/>
        <property name="defaultDestination" ref="destinationTopic"/>    <!-- 加载主题bean对象 -->
        <property name="messageConverter">
            <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
        </property>
    </bean>
</beans>

(2)范例

图片

图片

3.消息发布者【生产】

(1)代码块

package com.fengmo.activemq.topic;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import javax.jms.TextMessage;
@Service
/**
 * 主题的订阅者【生产】
 */
public class SpringMQ_Produce_Topic {
    @Autowired
    private JmsTemplate jmsTemplate;    //需要在配置文件配置bean对象
    public static void main(String[] args) {
        //1.加载依赖
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.获取该类的bean对象,注意参数第一个字母小写
        SpringMQ_Produce_Topic produce =(SpringMQ_Produce_Topic)applicationContext.getBean("springMQ_Produce_Topic");
        //3.获取jmsTemplate对象发送消息
        //3.1 原版写法
//        produce.jmsTemplate.send(new MessageCreator() {
//            @Override
//            public Message createMessage(Session session) throws JMSException {
//                //创建消息
//                TextMessage message = session.createTextMessage("发送一个字符串类型的消息");
//                return message;
//            }
//        });
        //3.2 lambda表达式
        produce.jmsTemplate.send((session -> {
            //创建消息
            TextMessage message = session.createTextMessage("spring和activemq的整合");
            return message;
        }));
        System.out.println("*****消息发送完成****");
    }
}

(2)范例

图片

图片

4.消息订阅者【消费】

(1)代码块

package com.fengmo.activemq.topic;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
/**
 * 主题的订阅者【消费】
 */
@Service
public class SpringMQ_Consumer_Topic {
    @Autowired
    private JmsTemplate jmsTemplate;
    public static void main(String[] args) {
        //1.加载依赖
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.获取该类的bean对象,注意参数第一个字母小写
        SpringMQ_Consumer_Topic consumer =(SpringMQ_Consumer_Topic)applicationContext.getBean("springMQ_Consumer_Topic");
        //3.获取jmsTemplate对象获取消息
        String message = (String) consumer.jmsTemplate.receiveAndConvert();
        System.out.println("这是接受的字符串" + message);
    }
}

(2)范例

图片

5.注意事项

1.操作的消费类和生产类都是同步操作,而非异步操作。如果异步操作,请看监听配置。

2.主题启动需要先启动消息订阅者【消息消费类】,再启动消息发布者【消息生产类】,不然,生产出来的消息都是废消息。

五.监听器配置【设置异步操作】

1.applicationContext.xml配置文件

(1)代码块

<?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:component-scan base-package="com.fengmo.activemq"/>
    <!-- 配置生产者 -->
    <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
        <property name="connectionFactory">
            <!-- 真正可以产生Connection的ConnectionFactory,由对应的JMS服务厂商提供 -->
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="tcp://192.168.83.130:61616"/>
            </bean>
        </property>
        <property name="maxConnections" value="100"></property>
    </bean>
    <!-- 这个是队列目的地,点对点的。注意:该bean多个配置就可以根据需求切换目的地 -->
<!--    <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">-->
<!--        <constructor-arg index="0" value="spring-active-queue"/>-->
<!--    </bean>-->
    <!-- Spring提供的JMS工具类,它可以进行消息发送、接受等【队列】 -->
<!--    <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="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg index="0" value="spring-active-topic"/>
    </bean>
    <!-- Spring提供的JMS工具类,它可以进行消息发送、接受等 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsFactory"/>
        <property name="defaultDestination" ref="destinationTopic"/>    <!-- 加载主题bean对象 -->
        <property name="messageConverter">
            <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
        </property>
    </bean>
    <!-- 第一种:加载自定义监听类的方式。第二种:在自定义监听类声明@Component -->
<!--    <bean id="myMessageListener" class="com.fengmo.activemq.MyMessageListener"/>-->
    <!-- 配置监听程序 -->
    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="jmsFactory"/>
        <property name="destination" ref="destinationTopic"/>
        <!-- public class MyMessageListener implements MessageListener -->
        <property name="messageListener" ref="myMessageListener"/>
    </bean>
</beans>

(2)范例

图片

图片

图片

2.自定义监听器

(1)代码块

package com.fengmo.activemq;
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(null != message && message instanceof TextMessage){
            TextMessage textMessage = (TextMessage) message;    //根据消息发送的数据类型进行强制
            try {
                System.out.println("监听器获取的消息是:" + textMessage.getText());  //输出内容
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}

(2)范例

图片

六.源码

spring-activemq.rar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值