下载并安装ActiveMQ服务器端
1:从http://activemq.apache.org/download.html下载最新的ActiveMQ
2:直接解压,然后拷贝到你要安装的位置就好了
启动运行
1:普通启动:到ActiveMQ/bin下面,./activemq start
2:启动并指定日志文件 ./activemq start > /tmp/activemqlog
检查是否已经启动
ActiveMQ默认采用61616端口提供JMS服务,使用8161端口提供管理控制台服务,执行以下命令以便检验是否已经成功启动ActiveMQ服务:
1:比如查看61616端口是否打开: netstat -a | grep 61616
2:也可以直接查看控制台输出或者日志文件
3:还可以直接访问ActiveMQ的管理页面:http://192.168.1.106:8161/admin/
默认的用户名和密码是admin/admi
关闭ActiveMQ,可以用./activemq stop
暴力点的可以用ps -ef | grep activemq 来得到进程号,然后kill掉
基本的Queue消息发送
配置Maven所需的依赖,示例如下:
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.9.0</version> </dependency> <dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>3.16</version> </dependency>
Queue消息发送的示例代码如下:
public class JmsSend { public static void main(String[] args) throws Exceptio { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.1.106:61616"); Connectio connectio = connectionFactory.createConnection(); connection.start (); Sessio sessio = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE) ; Destinatio destinatio = session.createQueue ("my-queue"); MessageProducer producer = session.createProducer(destination); for(int i=0; i<3; i++) { TextMessage message = session.createTextMessage ("message--"+i) ; Thread.sleep (1000) ; //通过消息生产者发出消息 producer.send(message); } session.commit() ; session.close(); connection.close (); } }
基本的Queue消息接收
public class JmsReceiver { public static void main(String[] args) throws Exceptio { ConnectionFactory cf = new ActiveMQConnectionFactory ("tcp://192.168.1.106:61616") ; Connectio connectio = cf.createConnection() ; connection.start (); final Sessio sessio = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Destinatio destinatio = session.createQueue ("my-queue") ; MessageConsumer consumer = session.createConsumer(destination); int i=0; while(i<3) { i++; TextMessage message = (TextMessage) consumer.receive(); session.commit(); System.out.printl ("收到消 息:" + message.getText()) ; } session.close(); connection.close (); } }
文档免费下载:ActiveMQ系列:ActiveMQ快速上手
http://download.csdn.net/detail/undoner/8302247