ActiveMQ安装和使用

本文详细介绍了ActiveMQ的安装步骤,包括从官网下载、解压、启动及访问管理界面,同时提供了SpringBoot项目中集成ActiveMQ的方法,涵盖了依赖引入、配置、消息生产与消费的实现。

一,安装

1,官网下载地址

http://activemq.apache.org/download.html

2,上传解压

tar -zxvf apache-activemq-5.15.4-bin.tar.gz

3,启动

cd  ~/apache-activemq-5.15.4/bin

./activemq start

4,访问activemq admin web应用

http://服务器IP:8161/admin/
默认用户名和密码都是admin
在这里插入图片描述

二,SpringBoot中的使用

1,引入依赖

<!-- activemq -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
    <version>1.5.6.RELEASE</version>
</dependency>

2,application.yml配置

server:
  port: 8080
  context-path: /
spring:
  activemq:
    broker-url:  tcp://10.2.255.102:61616
    user: admin
password: admin

3,消息生产者 activemq-provider

package demo.activemq.service;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Destination;

/**
 * 
 * 消息生产者
 */
@Service
public class ProducterService {
	
	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;

	public void sendMessage(String message) {
		//创建一个消息列队
		Destination destination = new ActiveMQQueue("msg");
		jmsMessagingTemplate.convertAndSend(destination, message);
		System.out.println("发送消息:" + message);
	}

	public void sendMessage1(String message) {
		//创建一个消息列队
		Destination destination = new ActiveMQQueue("msg1");
		jmsMessagingTemplate.convertAndSend(destination, message);
		System.out.println("发送消息:" + message);
	}
	
	public void sendMessage2(String message) {
		//创建一个消息列队
		Destination destination = new ActiveMQQueue("msg2");
		jmsMessagingTemplate.convertAndSend(destination, message);
		System.out.println("发送消息:" + message);
	}
}

测试类

package demo.activemq.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import demo.activemq.ActivemqProviderApplication;
import demo.activemq.service.ProducterService;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ActivemqProviderApplication.class)
public class SendMsgTest {
	
	@Autowired
	private ProducterService producterService;

	@Test
	public void test() {
		producterService.sendMessage("msg: hello activemq");
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		producterService.sendMessage1("msg1: hello");
		producterService.sendMessage1("msg1: activemq");
		
		producterService.sendMessage2("msg2: activemq");
	}

}

4,消息消费者 activemq-consume

package demo.activemq.service;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

/**
 * 
 * 消息的消费者
 */
@Service
public class ConsumerService {
	
	@JmsListener(destination = "msg")
	public void receiveQueueMsg(String text) {
		System.out.println("收到msg1消息:" + text);
	}
	
	@JmsListener(destination = "msg1")
	public void receiveQueueMsg1(String text) {
		System.out.println("收到msg1消息:" + text);
	}
	
    @JmsListener(destination = "msg2")
	public void receiveQueueMsg2(String text) {
		System.out.println("收到msg2消息:" + text);
	}

}

5,测试结果在这里插入图片描述在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值