然后在/usr/local/soft/apache-activemq-5.12.1/bin下有一个env文件,编辑此文件设置对应的jdk目录,要说明的是,新版本的activemq已经不支持jdk1.7以下,亲测不支持6u45,所以还是尽量在jdk1.7版本以上吧,最后一行(JAVACMD="auto")上面添加
JAVA_HOME="/usr/lib/jvm/jdk1.7.0_65"
进入到bin目录下,使用sudo ./activemq start
出现如下信息说明启动成功,即可访问管理界面:http://localhost:8161/admin/,默认的用户名与密码一般都是admin
package cn.example.activemq; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; /** * @author * @company * @date */ public class Sender { private static final int SEND_NUM=5; /** * 发送消息 * @param session * @param messageProducer */ public static void sendMessage(Session session,MessageProducer messageProducer) { for (int i = 0; i < SEND_NUM; i++) { TextMessage message = null; try { //创建消息 message = session.createTextMessage("ActiveMQ发送消息:" + i); System.out.println("发送消息:" + "ActiveMQ发送消息" + i); messageProducer.send(message); } catch (JMSException e) { e.printStackTrace(); } } } public static void main(String[] args){ //ConnectionFactory 连接工厂,JMS用它建立工厂 ConnectionFactory connectionFactory; //Connection是JMS客户端连接Provider Connection connection=null; //一个接收或者发送消息的线程 Session session; //消息的目的地 Destination destination; //消息发送者 MessageProducer messageProducer; //构造ConllectionFactory的实例对象,使用ActiveMQ connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); try { //构造connection connection = connectionFactory.createConnection(); connection.start(); //获取连接 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); //通过链接创建队列给消息目的地 destination = session.createQueue("FirstQueue"); //创建消息发送者 messageProducer = session.createProducer(destination); //设置不持久化 messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); sendMessage(session,messageProducer); session.commit(); }catch (JMSException e) { e.printStackTrace(); } finally { if(null!=connection){ try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } }
package cn.example.activemq; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; import java.util.logging.Logger; /** * @author * @company * @date */ public class Receiver { public static void main(String[] args) { //ConnectionFactory连接工厂 ConnectionFactory connectionFactory; //是JMS客户端连接JMS Provider:他是用来处理消息路由与传递的消息 Connection connection=null; //创建连接 Session session; //消息目的地 Destination destination; //消息接收者 MessageConsumer messageConsumer; connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); try { connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); destination = session.createQueue("FirstQueue"); messageConsumer = session.createConsumer(destination); while(true){ TextMessage textMessage = (TextMessage) messageConsumer.receive(500000); if(null!=textMessage){ System.out.println("收到消息:" + textMessage.getText()); }else{ break; } } } catch (JMSException e) { e.printStackTrace(); }finally { if(null!=connection){ try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } }
上面的500000代表500秒,当然没必要设置这么长时间,你可以同时启动4台Receiver,然后启动Sender,会出现如下情况,这就是点对点的效果
同时在web页面你会看到如下效果
package cn.example.activemq.topic; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; /** * Created by */ public class TopicPublisher { public static void main(String[] args) throws InterruptedException { //JMS建立工厂 ActiveMQConnectionFactory connectionFactory; //JMS客户端连接Provider Connection connection = null; //接受发送消息的线程 Session session = null; //主题定义 Topic topic; connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616"); try { connection = connectionFactory.createConnection(); connection.start(); session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createTopic("mq.topic"); MessageProducer producer = session.createProducer(destination); //消息的创建以及发送 for (int i = 1; i <= 20; i++) { MapMessage message = session.createMapMessage(); message.setLong("count", i); producer.send(message); System.out.println("发布者发送的消息"+i); } session.commit(); session.close(); } catch (JMSException e) { e.printStackTrace(); } finally { if(null!=session){ try { session.close(); } catch (JMSException e) { e.printStackTrace(); } } if(null!=connection){ try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } }
package cn.example.activemq.topic; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; /** * Created by */ public class TopicSubscriber { public static void main(String[] args) { ActiveMQConnectionFactory connectionFactory; Connection connection=null; connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616"); try { connection = connectionFactory.createConnection(); connection.start(); final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic("mq.topic"); final MessageConsumer consumer = session.createConsumer(topic); while(true){ consumer.setMessageListener(new MessageListener() { public void onMessage(Message message) { MapMessage mapMessage = null; try { mapMessage = (MapMessage) message; System.out.println("订阅者接收到消息:" + mapMessage.getLong("count")); } catch (JMSException e) { e.printStackTrace(); } } }); } } catch (JMSException e) { e.printStackTrace(); }finally { if(null!=connection){ try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } } }