activeMQ

1.从官网下载activeMQ,网址http://activemq.apache.org/activemq-5141-release.html,随便找一个版本下载都行,windows点击一下连接下载。
2.启动activeMQ,进入到下载的目录,点击activemq.bat,进行启动,
3.如果出现启动失败,一般是因为端口被占用的原因,win10端口被占用的解决方法如下:https://jingyan.baidu.com/article/3c48dd34491d47e10be358b8.html,我一开始是端口被占用启动失败的,然后一切问题解决。

4.在浏览器输入http://localhost:8161/进入后台管理页面,输入账号密码admin  admin (默认)

5.开始撸代码,provider和consumer的代码如下:




public class DNProviderImpl implements DNProvider{
 private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
 private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;
 ConnectionFactory connectionFactory;
 
 Connection connection;
 
 Session session;
 public void init() {
  
  try {
   connectionFactory  = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKERURL);
   connection = connectionFactory.createConnection();
   connection.start();
   session = connection.createSession(true, Session.SESSION_TRANSACTED);
  } catch (JMSException e) {
   e.printStackTrace();
  }  
 }
 public void sendMessage(String disName) {
  //创建队列
  try {
   Queue queue = session.createQueue(disName);
   MessageProducer messageProducer = session.createProducer(queue);
   for(int i=0;i<10;i++){
    TextMessage tm = session.createTextMessage("provider activeMq 发送短信,内容是content");
    System.out.println("provider activeMq 发送短信,内容是content");
    //发送消息
    messageProducer.send(tm);
    //提交
    session.commit();
    
   }
   
  } catch (JMSException e) {
   e.printStackTrace();
  }
 }
}




package com.dongnao.jack.queue.consumer;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**  
 * @ClassName:     DNConsumerImpl  
 * @Description:   TODO 
 * @author:        Taurus
 * @date:          2018年1月4日 下午5:29:08    
 */
public class DNConsumerImpl implements DNConsumer {
 private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
 private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL;
 ConnectionFactory connectionFactory;
 
 Connection connection;
 
 Session session;
 public void init() {
  
  try {
   connectionFactory  = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKERURL);
   connection = connectionFactory.createConnection();
   connection.start();
   session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
  } catch (JMSException e) {
   e.printStackTrace();
  }  
 }
 public void getMessage(String disName) {
       try {
  Queue queue = session.createQueue(disName);
  MessageConsumer messageConsumer = session.createConsumer(queue);
  while(true){
   //接收消息
   TextMessage message = (TextMessage) messageConsumer.receive();
   Thread.sleep(1000);
   if(message!=null){
    System.out.println("consumer :我是消费者,我接收的消息内容是:"+message.getText());
   }else{
    break;
   }
  }
 } catch (JMSException | InterruptedException e) {
  e.printStackTrace();
 }
      
 }
}



6.编写测试类

public class ProviderTest {
 public static void main(String[] args) {
  DNProvider provider = new DNProviderImpl();
  provider.init();
  provider.sendMessage("DN_JACK_20");
 }
}


public class ConsumerTest {
 public static void main(String[] args) {
  DNConsumer consumer = new DNConsumerImpl();
  consumer.init();
  consumer.getMessage("DN_JACK_20");
 }
}



7.实验结果


8.activeMQ后台查看,消息名称,消费者个数都一 一显示了



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值