<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
</dependency>
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> </dependency>
# activemq 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
package com.activemq.queue;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.Queue;
@Configuration
public class QueueConfig {
@Bean
public Queue logQueue() {
return new ActiveMQQueue(QueueName.LOG_QUEUE);
}
}
package com.activemq.producer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
@Component
public class LogProducer implements CommandLineRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(LogProducer.class);
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue logQueue;
@Override
public void run(String... strings) throws Exception {
send("This is a log message.");
LOGGER.info("Log Message was sent to the Queue named sample.log");
}
public void send(String msg) {
this.jmsMessagingTemplate.convertAndSend(this.logQueue, msg);
}
}
package com.activemq.consumer;
import com.activemq.queue.QueueName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class LogConsumer {
private static final Logger LOGGER = LoggerFactory.getLogger(LogConsumer.class);
@JmsListener(destination = QueueName.LOG_QUEUE)
public void receivedQueue(String msg) {
LOGGER.info("Has received from " + QueueName.LOG_QUEUE + ", msg: " + msg);
}
}
@Autowired
private LogProducer logProducer;
@GetMapping("/activemq/send")
public String activemq(HttpServletRequest request, String msg) {
msg = StringUtils.isEmpty(msg) ? "This is Empty Msg." : msg;
try {
logProducer.send(msg);
} catch (Exception e) {
e.printStackTrace();
}
return "Activemq has sent OK.";
}
@Configuration//配置控制
@EnableAutoConfiguration//启用自动配置
@ComponentScan//组件扫描
@EnableConfigurationProperties({EmailProp.class})
@EnableJms
public class Bootstrap {
private static final Logger LOGGER = LoggerFactory
.getLogger(Bootstrap.class);
public static void main(String[] args) throws Exception {
SpringApplication.run(Bootstrap.class, args);
LOGGER.info("Server running...");
}
}
INFO 1498 --- [enerContainer-1] c.j.a.activemq.consumer.LogConsumer : Has received from sample.log, msg: test log [DefaultMessageListenerContainer-1] INFO c.j.a.activemq.consumer.LogConsumer - Has received from sample.log, msg: test log
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有