ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。支持很多跨语言的客户端和协议,例如 Java, C, C++, C#, Ruby, Perl, Python, PHP。支持的协议有OpenWire ,Stomp,AMQP,MQTT。
Unix安装启动
选择要下载的ActiveMQ 版本,名称类似于 activemq-x.x.x-tar.gz,然后启动
tar zxvf activemq-x.x.x.tar.gz
cd [activemq-install_dir]/bin
chmod 755 activemq
cd [activemq_install_dir]
bin\activemq start
监控ActiveMQ运行状态的地址
http://localhost:8161/admin
停止ActiveMQ
cd [activemq_install_dir]/bin
./activemq stop
JAVA客户端代码
发送消息到ActiveMQ队列的代码:
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public final class ActiveMQSample {
public static void main(final String[] args) throws Exception {
final ConnectionFactory connFactory = new ActiveMQConnectionFactory("tcp://192.168.10.201:61616");
final Connection conn = connFactory.createConnection();
final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination dest = sess.createQueue("SampleQueue");
final MessageProducer prod = sess.createProducer(dest);
final Message msg = sess.createTextMessage("Simples Assim");
prod.send(msg);
conn.close();
}
}
发送消息到 ActiveMQ主题的代码:
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public final class ActiveMQSample {
public static void main(final String[] args) throws Exception {
final ConnectionFactory connFactory = new ActiveMQConnectionFactory("tcp://192.168.10.201:61616");
final Connection conn = connFactory.createConnection();
final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination dest = sess.createTopic("SampleTopic");
final MessageProducer prod = sess.createProducer(dest);
final Message msg = sess.createTextMessage("Simples Assim");
prod.send(msg);
conn.close();
}
}
从ActiveMQ队列接收消息的代码:
package com.zhuyun.consumer;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQConsumer {
public static void main(String[] args) throws Exception{
final ConnectionFactory connFactory = new ActiveMQConnectionFactory("tcp://192.168.10.201:61616");
final Connection conn = connFactory.createConnection();
conn.start();
final Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination dest = session.createQueue("SampleQueue");
final MessageConsumer consumer = session.createConsumer(dest);
Message message = consumer.receive();
conn.close();
}
}
从ActiveMQ主题接收消息的代码:
package com.zhuyun.consumer;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ActiveMQConsumer {
public static void main(String[] args) throws Exception{
final ConnectionFactory connFactory = new ActiveMQConnectionFactory("tcp://192.168.10.201:61616");
final Connection conn = connFactory.createConnection();
conn.start();
final Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination dest = session.createTopic("SampleTopic");
final MessageConsumer consumer = session.createConsumer(dest);
Message message = consumer.receive();
conn.close();
}
}