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());
}
}
}
启动顺序
- ActiveMQ server
- ReceiveMessage
- 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