ActiveMQ入门实例,以及类封装

这篇文章适合已经搭建好了activeMQ环境的人,需要封装下activeMQ基本功能的人。封装的不好,仅作参考。
package com.quhuhu.sync.util;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
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 liang.he
 *
 */
public class ActiveMqCreater {
    //queue的名字,这个对应你在activeMQ上面创建的Queue的名字
    public static String subject = "FirstQueue";
    // ConnectionFactory :连接工厂,JMS 用它创建连接
    public static ConnectionFactory connectionFactory = null;
    // Connection :JMS 客户端到JMS Provider 的连接
    public static Connection connection = null;
    // Session: 一个发送或接收消息的线程
    private Session session = null;
    // Destination :消息的目的地;消息发送给谁.
    public static Destination destination = null;
    // MessageProducer:消息发送者
    private MessageProducer producer = null;
    //MessageConsumer : 消息接收者
    private MessageConsumer receiver = null;
    
    static {
        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 
     * @return 初始化消息发送者
     * @throws Exception
     */
    public MessageProducer initMessageProducer() throws Exception{
        session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        //消息目的地
        destination = session.createQueue(subject);
        // 得到消息【发送者】
        producer = session.createProducer(destination);
        // 设置不持久化
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        return producer;
    }
    
    /**
     * 
     * @return 初始化消息接收者
     * @throws JMSException
     */
    public MessageConsumer initMessageConsumer() throws JMSException {
        session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
        //消息目的地
        destination = session.createQueue(subject);
        return session.createConsumer(destination);
    }
     
    /**
     * 消息发送方法
     * @param msg 消息
     * @throws JMSException
     */
    public void sendMsg(String msg) throws JMSException {
        try {
            System.out.println("fasongle ");
            initMessageProducer().send(session.createTextMessage(msg));
            session.commit();
            System.out.println("发送消息:" + msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 消息接受方法
     * @return msg 消息
     * @throws JMSException
     */
    public String receiveMsg(MessageConsumer consumer) throws JMSException {
        TextMessage message = (TextMessage)consumer.receive();
        if (null != message) {
            return message.getText();
        }
        return null;
    }
    
    /**
     * 消息接受方法
     * @return msg 消息
     * @throws JMSException
     */
    public String receiveMsg(MessageConsumer consumer, long time) throws JMSException {
        TextMessage message = (TextMessage)consumer.receive(time);
        if (null != message) {
            return message.getText();
        }
        return null;
    }
    
    /**
     * 释放MessageConsumer资源
     * @throws JMSException
     */
    public void close(MessageConsumer receiver) throws JMSException {
        System.out.println("close JMSconnection!");
        if(null != receiver) {
            receiver.close();
        }
    }
    
    /**
     * 释放MessageProducer资源
     * @throws JMSException
     */
    public void close(MessageProducer producer) throws JMSException {
        System.out.println("close JMSconnection!");
        if(null != producer) {
            producer.close();
        }
    }
    
    /**
     * 释放Session资源
     * @throws JMSException
     */
    public void close(Session session) throws JMSException {
        System.out.println("close JMSconnection!");
        if(null != session) {
            session.close();
        }
    }
    
    /**
     * 释放Connection资源
     * @throws JMSException
     */
    public void close(Connection connection) throws JMSException {
        System.out.println("close JMSconnection!");
        if(null != connection) {
            connection.close();
        }
    }
    
}
封装的ActiveMQ类
下面是测试接受类:
import javax.jms.JMSException;
import javax.jms.MessageConsumer;

public class TestReceive {
    public static void main(String[] args) throws JMSException {
        JMSUtil js = new JMSUtil();
        MessageConsumer consumer = js.initMessageConsumer();
        while (true) {
            //设置接收者接收消息的时间,为了便于测试,这里谁定为100s
            System.out.println("收到消息" + js.receiveMsg(consumer, 50000));
        }
    }
}


下面是测试发送类:

import javax.jms.JMSException;

public class TestSend {
    public static void main(String[] args) throws JMSException {
        JMSUtil js = new JMSUtil();
        js.sendMsg("11111111111111ssssssssssssssssssssssassssssssssss");
        js.sendMsg("2222222222sssssssssssssssssss2ssssssssss");
        js.sendMsg("333333333333sssssssssssssssssssdssssssssss");
        js.sendMsg("444444444sssssssssssssssssssssssssssss");
        js.sendMsg("55555555555ssssssssss2sssssssssssssssssss");
        js.sendMsg("666666666666sssssssssssss3ssssssssssssssss");
    }
}

运行的时候先运行接收类,在运行发送类;



activemq的环境自行百度,很简单,我这里是使用的activeMQ的QUEUE消息队列,activeMQ还提供topic,感兴趣的可以自己研究下他们的区别与用法。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值