spring boot 集成activeMq

下载activeMq

下载地址

解压,在apache-activemq-5.15.12\bin\win64目录下直接命令运行:

activemq.bat start

结果发现报错:

 于是就去找谁占了我61616端口

netstat -ano|findstr "61616"
tasklist|findstr "28068"

扫描二维码关注公众号,回复: 10780485 查看本文章

 结果居然是pycharm,服了

把pycharm关了,再启动:

 ???搞我心态???

又来一个端口被占:

 莫名其妙的服务。

好吧原来是我有rabbitMq在运行,把它关了。

 再次启动,终于成功,访问http://localhost:8161/admin/

用户名密码都是admin:

spring boot 集成很简单:

首先引入依赖:

<!--ActiveMq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

application.yml新增:

spring
  activemq:   broker
-url: tcp://127.0.0.1:61616   user: admin   password: admin    close-timeout: 15s # 在考虑结束之前等待的时间   in-memory: true # 默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此值。   non-blocking-redelivery: false # 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。   send-timeout: 0 # 等待消息发送响应的时间。设置为0等待永远。   pool:   enabled: false

我们就不使用什么连接池了。

新增配置类

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

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

/**
 * 功能描述: <br> 配置activeMq
 * @Author: Lenovo
 * @Date: 2020/4/10 15:22
 */
@Configuration
@EnableJms
public class ActiveMqConfig {
    @Bean
    public Queue queue() {
        return new ActiveMQQueue("springboot.queue") ;
    }

    //springboot默认只配置queue类型消息,如果要使用topic类型的消息,则需要配置该bean
    @Bean
    public JmsListenerContainerFactory jmsTopicListenerContainerFactory(ConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        //这里必须设置为true,false则表示是queue类型
        factory.setPubSubDomain(true);
        return factory;
    }

    @Bean
    public Topic topic() {
        return new ActiveMQTopic("springboot.topic") ;
    }
}

生产者(注意只有部分代码):

import javax.jms.Queue;
import org.springframework.jms.core.JmsMessagingTemplate;

@Autowired
    private JmsMessagingTemplate jmsTemplate;
    @Autowired
    private Queue queue;

jmsTemplate.convertAndSend(queue, “msg”);

消费者(注意只有部分代码):

@JmsListener(destination="springboot.queue")
    public void ListenQueue(String msg)  {

        try {
            System.out.println("接收到queue消息:" + msg);
            
    }

猜你喜欢

转载自www.cnblogs.com/SunSAS/p/12696444.html