您的位置:首页 > 编程语言 > Java开发

spring boot jms activemq

2016-10-20 20:39 429 查看
一.创建maven工程,编写pom.xml文件

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath />
</parent>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>二.创建Application.java程序入口类
@SpringBootApplication
public class Application {

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

}三.配置ActiveMQConfig
@Configuration
@EnableJms
public class ActiveMQConfig {
@Bean//配置一个消息队列
public Queue queue() {
return new ActiveMQQueue("sample.queue");
}
}四.创建MQProduceService
@Service
public class MQProduceService {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;

@Autowired
private Queue queue;

public void send(String msg) {//向指定队列中发送消息
this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
}
}五.创建MQConsumerService
@Service
public class MQConsumerService {

private String text;

@JmsListener(destination = "sample.queue")//监听指定消息队列
public void receiveQueue(String text) {
this.text = text;
System.out.println(text);
}

public String receive() {
return text;
}
}

六.创建MQController
@RestController
public class MQController {

@Autowired
private MQProduceService produceService;

@Autowired
private MQConsumerService consumerService;

@RequestMapping("/send")
public String send() {
produceService.send("this is an activemq message");
return "send";
}

@RequestMapping("/receive")
public String receive() {
String str = consumerService.receive();
return str;
}
}七.创建配置文件application.properties
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
八.验证
启动ActiveMQ服务,可以看到broker-url.然后启动工程,打开浏览器访问http://localhost:8080/send,然后发现控制台打印出了发送的message。当然打开activemq web console也可以得到验证。

1.启动AvtiveMQ服务



2.访问http://localhost:8080/send发送一个message



3.控制台输出信息



4.打开activemq web控制台查看



源代码链接:https://github.com/wangjianyangchn/SpringBootProject/tree/master/JMS
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: