JMS学习之ActiveMQ-简单使用

1.下载 ActiveMQ 包在ActiveMQ 官网

下载可以去官网下载:http://activemq.apache.org/download.html

2、启动ActiveMQ

启动ActiveMQ非常的简单。进入bin目录
双击“activemq.bat”就启动运行了。因为ActiveMQ是用Java编写的,所以必须确保电脑已经安装了JDK。

双击运行之后就可以在浏览器里面输入:http://localhost:8161/admin。就能够进入ActiveMQ的后台页面了。帐号密码默认都是:admin
Java程序连接端口:61610

修改61610端口,可以修改文件conf>activemq.xml
修改登录名和密码,可以修改文件conf>jetty-realm.properties

3. ActiveMQ 相关组件的关系

在这里插入图片描述

4.测试代码

pom.xml

		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-core</artifactId>
			<version>5.5.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-broker</artifactId>
			<version>5.14.5</version>
		</dependency>
		
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.21</version>
		</dependency>

消费者代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ReceiveMessage {
	private static final String url = "tcp://localhost:61610";
	private static final String QUEUE_NAME = "TestQueue";

	public void receiveMessage() {
		Connection connection = null;
		try {
			try {
				ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
				connection = connectionFactory.createConnection();
			} catch (Exception e) {
				e.printStackTrace();
			}
			connection.start();
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue(QUEUE_NAME);
			MessageConsumer consumer = session.createConsumer(destination);
			consumeMessagesAndClose(connection, session, consumer);
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}

	protected void consumeMessagesAndClose(Connection connection, Session session, MessageConsumer consumer)
			throws JMSException {
		for (int i = 0; i < 1;) {
			Message message = consumer.receive(1000);
			if (message != null) {
				i++;
				onMessage(message);
			}
		}
		System.out.println("Closing connection");
		consumer.close();
		session.close();
		connection.close();
	}

	public void onMessage(Message message) {
		try {
			if (message instanceof TextMessage) {
				TextMessage txtMsg = (TextMessage) message;
				String msg = txtMsg.getText();
				System.out.println("Received: " + msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String args[]) {
		ReceiveMessage rm = new ReceiveMessage();
		rm.receiveMessage();
	}

}

生产端

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;

public class SendMessage {
	private static final String url = "tcp://localhost:61610";
	private static final String QUEUE_NAME = "TestQueue";
	protected String expectedBody = "<hello>world!</hello>";

	public void sendMessage() throws JMSException {
		Connection connection = null;
		try {
			ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
			connection = (Connection) connectionFactory.createConnection();
			connection.start();
			Session session = (Session) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination destination = session.createQueue(QUEUE_NAME);
			MessageProducer producer = session.createProducer(destination);
			TextMessage message = session.createTextMessage(expectedBody);
			message.setStringProperty("headname", "remoteB");
			producer.send(message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		SendMessage sndMsg = new SendMessage();
		try {
			sndMsg.sendMessage();
		} catch (Exception ex) {
			System.out.println(ex.toString());
		}
	}
}

启动顺序

  1. ActiveMQ server
  2. ReceiveMessage
  3. SendMessage

最后访问admin page 你就会发现有个TestQueue 自动创建了
在这里插入图片描述

如果你不想用activemq.bat启动,你也可以用Java代码之前启动一个broker.不过这样就不能进Admin page 了

代码如下

package com.jms.test;
import org.apache.activemq.broker.BrokerService;

public class StartActiveMQBroker 
{
    public static void main( String[] args )throws Exception
    {
	 BrokerService broker = new BrokerService();
	        try {
	                        broker.setUseJmx(false);
	                        broker.addConnector("tcp://localhost:" + 61610);
//	                                                    broker.setPassiveSlave(true);
	                        broker.start();
	        } catch (Exception e) {
	                        throw new RuntimeException(e);
	        }
	        for(int i =0 ;i<30 ; i++) {
	                        System.out.println();
	                        Thread.sleep(24 * 60 * 60 * 1000);
	        }
    }
}

如果遇到下面异常

javax.jms.JMSException: Failed to build body from content. Serializable class not available to broker. Reason: java.lang.ClassNotFoundException: Forbidden class com.* This class is not trusted to be serialized as ObjectMessage payload. Please take a look at http://activemq.apache.org/objectmessage.html for more information on how to configure trusted classes.

解决方法
在你启动的系统里加下面系统参数就可以了

-Dorg.apache.activemq.SERIALIZABLE_PACKAGES=*

参考资料
http://activemq.apache.org/objectmessage.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茫茫人海一粒沙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值