spring,springboot整合ActiveMQ

spring与activemq的整合:

   jar包

          <dependency>
              <!--activemq的jar-->
              <groupId>org.apache.activemq</groupId>
              <artifactId>activemq-all</artifactId>
              <version>5.15.9</version>
          </dependency>
          <!--整合spring-->
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context</artifactId>
              <version>5.2.5.RELEASE</version>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-jms</artifactId>
              <version>5.2.5.RELEASE</version>
          </dependency>
          <dependency>
              <groupId>org.apache.activemq</groupId>
              <artifactId>activemq-pool</artifactId>
              <version>5.15.15</version>
          </dependency>
          <dependency>
              <groupId>org.apache.xbean</groupId>
              <artifactId>xbean-spring</artifactId>
              <version>3.16</version>
          </dependency>

配置文件

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/task"
       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
                            http://www.springframework.org/schema/task https://www.springframework.org/schema/task/spring-task.xsd
                           http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="com.atchengdu.Spring"></context:component-scan>
    <!--配置生产者-->
    <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
        <property name="connectionFactory">
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="tcp://192.168.150.128:61616"></property>
            </bean>
        </property>
        <property name="maxConnections" value="100"></property>
    </bean>
    <!--设置队列目的-->
  <!--<bean id="activeMQQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="spring-active-queue"></constructor-arg>
    </bean>-->
    <bean id="activeMQTopic" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg index="0" value="spring-active-queue"></constructor-arg>
    </bean>
    <!--配置监听器-->
    <bean id="container" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="pooledConnectionFactory"></property>
        <property name="destination" ref="activeMQTopic"></property>
        <!--mymessageListener的接口实现类-->
        <property name="messageListener" ref="mymessageListener"></property>
    </bean>
    <!--spring与activemq的整合-->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="pooledConnectionFactory">
        </property>
        <property name="defaultDestination" ref="activeMQTopic">
        </property>
        <property name="messageConverter">
            <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"></bean>
        </property>
    </bean>
</beans>

测试类

//生产者
@Service
public class Springmessage {
    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("appliction.xml");
        Springmessage springmessage = (Springmessage) context.getBean(Springmessage.class);
        springmessage.jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage("spring--active333");
                return textMessage;
            }
        });
        System.out.println("test  over");
    }
}

//消费者
@Service
public class Springconstmer {
    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("appliction.xml");
        Springconstmer springconstmer=context.getBean(Springconstmer.class);
        String message = (String) springconstmer.jmsTemplate.receiveAndConvert();
        System.out.println(message);
    }
}
//监听器
@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 (Exception e){
                 e.printStackTrace();
             }
         }
    }
}

springboot与activemq的整合:

jar

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
             <version>2.6.7</version>
        </dependency>

配置文件:

#Activemq配置
#username  and password 采用默认
spring.activemq.password=admin
spring.activemq.user=admin
#是否使用内嵌的MQ
spring.activemq.in-memory=false
#是否信任MQ的所有jar
spring.activemq.packages.trust-all=true
#MQ服务的ip:端口号
spring.activemq.broker-url=tcp://192.168.150.128:61616
#是否开启主题订阅
spring.jms.pub-sub-domain=true

测试类

/**
 * 消息消费者
 */
@Component
public class Comstmer {
    @JmsListener(destination = "springboot")
    public void resive( TextMessage textMessage) throws Exception{
        System.out.println("消息接受"+textMessage.getText());
    }
}

/**
 *消息生产者
 */
@Component
public class Produce {
    //    @Autowired
//    private Queue queue;
    @Autowired
    private Topic topic;
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

//    public void  send(String msg){
//        jmsMessagingTemplate.convertAndSend(queue,msg);
//    }

    /*   //**
     * 间隔时间投递
     * 3秒投一个消息
     *//*
    @Scheduled(fixedDelay = 3000)
    public  void  sendmessagebytrime(){
      jmsMessagingTemplate.convertAndSend(queue,"*****->"+ UUID.randomUUID().toString().substring(0,6));
        System.out.println("消息投递成功");
    }*/
    /**
     * 间隔时间投递
     * 3秒投一个消息
     */
    @Scheduled(fixedDelay = 3000)
    public void sendmessagebytrime() {
        jmsMessagingTemplate.convertAndSend(topic, "*****->" + UUID.randomUUID().toString().substring(0, 6));
        System.out.println("消息投递成功");
    }
}

//启动类
package com.atchengdu;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.jms.Queue;
import javax.jms.Topic;

@SpringBootApplication
@EnableJms
@EnableScheduling //开启定时投递
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

/*  @Bean   队列配置
    Queue queue(){
        return new ActiveMQQueue("springboot");
    }
    */
    @Bean
    public Topic topic(){
        return new ActiveMQTopic("springboot-topic");
    }

}

需要注意的是:Queue和Topic的jar是javax.jms中的,而不是java,util下的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值