SpringBoot整合ActiveMQ(生产者)

1、建立maven工程,导入dependency

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<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>
<artifactId>active_mq1</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--spring boot整合activemq的jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、编写配置文件
spring:
application:
name: active_mq_server
activemq:
broker-url: tcp://localhost:61616 #mq broker
user: admin
password: admin
jms:
pub-sub-domain: false #false队列 true主题
mymq: my-mq-test #queue 队列名称
server:
port: 9191

3、配置类
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;

import javax.jms.Queue;

@Component
@EnableJms
public class MQConfig {
@Value("${mymq}")
private String queueName;

@Bean
public Queue queue() {
return new ActiveMQQueue(queueName);
}
}

4、生产者

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Queue;

@Component
public class SP {

@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;

public void productMQ(){
jmsMessagingTemplate.convertAndSend(queue,"测试");
}

}



posted @ 2020-11-18 15:34  Covenant  阅读(224)  评论(0编辑  收藏  举报

ヾ(≧O≦)〃点我返回顶部嗷~