使用了springboot框架,直接上手的activeMQ
1. propertise基本配置:
spring.activemq.broker-url=tcp://XXXXX:XXX
spring.activemq.user=admin
spring.activemq.password=XXXXXX
#springboot自动根据这些配置进行注册**ActiveMQConnectionFactory**
和 **JmsTemplate**
#注意
#spring.jms.pub-sub-domain=true 注意开启后为订阅模式,
消费者就会跑到Topic上,即使你收听的是队列。
2. 在service上构建消费者和生产者。
Producer
@Service
public class Producer {
@Autowired
JmsMessagingTemplate jmsTemplate;
public void sendMessage(Destination destination, Message message){
jmsTemplate.convertAndSend(destination,message);
}
}
Consumer
@Component
@Slf4j
public class Consumer {
@HikActiveMQDiscovery
@JmsListener(destination = "hello")
public void receive(Message message) {
System.out.println("here is the message :" + message);
log.info("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!recevived!!!!!!!!!!!!!!!!!!");
}
}
3. Controller上发布消息(很鸡肋)
public class mqController {
// @Autowired
// JmsComponent jmsComponent;
@Autowired
Producer producer;
@Autowired
Consumer consumer;
@RequestMapping("/mq")
public void help() throws InterruptedException {
ActiveMQQueue destination = new ActiveMQQueue("hello");
for (int i = 0; i < 100; i++) {
Message me = new Message(i + "this is the activemq producer", new Date());
producer.sendMessage(destination, me);
Thread.sleep(1000);
log.info(i + "have sent!");
}
}
}