1.pom文件导入整合需要的依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
2.配置文件,配置连接端口,队列名称的内容
server:
port: 9001
spring:
activemq:
broker-url: tcp://192.168.29.128:61616
user: admin
password: admin
jms:
pub-sub-domain: false # false为队列 true为主题
myqueue: spring-boot-mq
3.书写bean类,初始化队列@EnableJms开启关于jms的功能
/**
* 初始化队列
*/
@Component
@EnableJms
public class ConfigBean {
@Value("${myqueue}")
private String mqqueue;
@Bean
public Queue mqQueue() {
return new ActiveMQQueue(mqqueue);
}
}
4.定时推送消息使用spring boot中的 @Scheduled(fixedDelay = 3000)语句
@Component
public class Produer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
@Scheduled(fixedDelay = 3000)
public void sendMessage() {
jmsMessagingTemplate.convertAndSend(queue, "黎明");
System.out.println("ssssssss");
}
}
5.书写启动类开启定时任务@EnableScheduling
@SpringBootApplication
@EnableScheduling
public class applicationRun {
public static void main(String[] args) {
SpringApplication.run(applicationRun.class,args);
}
}
消费者的话需要重新启动一个微服务对其spring boot也进行了很好的处理 使用这个@JmsListener注解进行监听
@JmsListener(destination = "${myqueue}")
public void recive(TextMessage textMessage) throws JMSException {
System.out.println(textMessage.getText());
}
网友评论