JAVA ActiveMQ

1.MQ消息处理插件  接口属于JMS(JAVA MESSAGE SERVICE) 规范,

2.官网下载地址:http://activemq.apache.org/

3.运行ActiveMQ

解压缩Apache-activeMq.zip,双击Apache-activemq\bin\activemq.bat运行ActiveMQ

4.登陆http://localhost:8161/admin/    默认用户名/密码

  admin/admin

5.将下载的lib 包中的activemq-all-5.13.3.jar 拷贝到项目lib中

6.点对点的消息模式

生产者代码

package com.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class JMSProducer {
    
    private static final String USERNAME=ActiveMQConnection.DEFAULT_USER;//默认的连接;
    private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;//默认的密码;
    private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL;//默认的链接地址;
    private static final int SENDNUM=10;//发送消息的数量;
    
    public static void main(String[] args) {
        ConnectionFactory connectionFactory;//链接工厂  
        Connection connect = null;//lianjie
        Session session;//会话接受或者发送消息的线程;
        Destination destination;//消息目的地;
        MessageProducer messageProducer;//消息生产者;
        
        connectionFactory = new ActiveMQConnectionFactory(JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL);
        
        try {
            connect = connectionFactory.createConnection();//通过链接工厂获取链接
            connect.start();//启动链接
            session = connect.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);//创建session
            destination = session.createQueue("FirstQueue2");//创建消息队列
            messageProducer = session.createProducer(destination);//创建消息生产者;
            sendMessage(session,messageProducer);
            session.commit();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(connect!=null){
                try {
                    connect.close();
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }
    
    public static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{
        for(int i=0;i<JMSProducer.SENDNUM;i++){
            TextMessage message = session.createTextMessage("ActiveMQ 发送的消息"+i);
            System.out.println("发送消息:"+"ActiveMQ 发送的消息"+i);
            messageProducer.send(message);
        }
    }
}

6.消费者代码


package com.java1234.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

/**
 *
 * @author zzx
 *
 */
public class JMSConsumer {
    private static final String USERNAME=ActiveMQConnection.DEFAULT_USER;//默认的连接;
    private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD;//默认的密码;
    private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL;//默认的链接地址;
    private static final int SENDNUM=10;//发送消息的数量;
    
    public static void main(String[] args) {
        ConnectionFactory connectionFactory;//链接工厂  
        Connection connect = null;//lianjie
        Session session;//会话接受或者发送消息的线程;
        Destination destination;//消息目的地;
        MessageConsumer messageConsumer;//消息消费者;
        
        connectionFactory = new ActiveMQConnectionFactory(JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL);
        
        try {
            connect = connectionFactory.createConnection();//通过链接工厂获取链接
            connect.start();//启动链接
            session = connect.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);//创建session
            destination = session.createQueue("FirstQueue1");//创建消息队列
            messageConsumer = session.createConsumer(destination);//创建消息消费者;
            while(true){
                TextMessage textMessage = (TextMessage) messageConsumer.receive(100*1000);
                if(textMessage!=null){
                    System.out.println("收到的消息:"+textMessage.getText());
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(connect!=null){
                try {
                    connect.close();
                } catch (JMSException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你微笑莳很美丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值