1.添加依赖
org.springframework.boot
spring-boot-starter-activemq
2.application.properties里配端口号
server.port=7001
spring.thymeleaf.mode = LEGACYHTML5
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo_5_30?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.activemq.broker-url=tcp://localhost:61616
3.Controller
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@RequestMapping("/send")
@ResponseBody
public String send(final String msg) {
jmsMessagingTemplate.convertAndSend("testQ", msg);
return "success";
}
4.前端
Hello
配监听
新建mq包,新建MyJmsListener类
package com.pp.mq;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class MyJmsListener {
@JmsListener(destination="testQ")
public void onMessage(Message message) throws JMSException {
TextMessage textMessage= (TextMessage) message;
System.out.println("MyJmsListener的消费任务:"+textMessage.getText());
}
}