Jfinal 中ActiveMQ 完整配置与使用

一、初始化连接工具


import javax.jms.JMSException;

import com.hegao.pdos.core.uitle.activeMQ.ActiveMQ;
import com.hegao.pdos.core.uitle.activeMQ.JmsReceiver;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnection;
import org.apache.activemq.pool.PooledConnectionFactory;

import com.jfinal.plugin.IPlugin;
/**
 * ActiveMQ插件
 * Created by huasheng on 2021/1/6.
 */
public class ActiveMQPlugin implements IPlugin {

    public static final String BROKER_URL = "tcp://localhost:61616";
    public static final String QUEUE_NAME = "sendMail";

    @Override
    public boolean start() {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setUserName(ActiveMQConnection.DEFAULT_USER);
        activeMQConnectionFactory.setPassword(ActiveMQConnection.DEFAULT_PASSWORD);
        activeMQConnectionFactory.setBrokerURL(BROKER_URL);
        activeMQConnectionFactory.setDispatchAsync(true);// 异步发送消息

        PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(activeMQConnectionFactory);
        pooledConnectionFactory.setMaximumActiveSessionPerConnection(200);
        pooledConnectionFactory.setIdleTimeout(120);
        pooledConnectionFactory.setMaxConnections(5);
        pooledConnectionFactory.setBlockIfSessionPoolIsFull(true);
        try {
            PooledConnection connection = (PooledConnection) pooledConnectionFactory.createConnection();
            connection.start();
            ActiveMQ.pooledConnectionMap.put(QUEUE_NAME, connection);
            new JmsReceiver(connection, QUEUE_NAME);
        } catch (JMSException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}



二、ActiveMQ工具类

/**
 * ActiveMQ工具类
 * Created by huasheng on 2021/1/6.
 */
import java.util.concurrent.ConcurrentHashMap;
import org.apache.activemq.pool.PooledConnection;

public class ActiveMQ {

    public static final ConcurrentHashMap<String, PooledConnection> pooledConnectionMap = new ConcurrentHashMap<>();

    public static void addConnection(String connectionName, PooledConnection connection) {
        pooledConnectionMap.put(connectionName, connection);
    }

    public static PooledConnection getConnection(String connectionName) {
        return pooledConnectionMap.get(connectionName);
    }
}

三、生产者


import javax.jms.*;

import org.apache.activemq.pool.PooledConnection;

/**
 * ActiveMQ生产者
 * Created by huasheng on 2021/1/6.
 */
public class JmsSender {

    private Session session;
    private MessageProducer producer;

    public JmsSender(PooledConnection connection, String subject) throws JMSException {
        // 事务性会话,自动确认消息
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 消息的目的地
        Queue queue = session.createQueue(subject);
        producer = session.createProducer(queue);
    }

    public Session getSession() {
        return session;
    }

    public void sendMessage(Message message) throws JMSException {
        producer.send(message);
    }
}



四、消费者

import javax.jms.*;

import org.apache.activemq.pool.PooledConnection;

/**
 * ActiveMQ消费者
 * Created by huasheng on 2021/1/6.
 */
public class JmsReceiver implements MessageListener {

    public JmsReceiver(PooledConnection connection, String subject) throws JMSException {
        // 事务性会话,自动确认消息
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 消息的目的地
        Queue queue = session.createQueue(subject);
        MessageConsumer consumer = session.createConsumer(queue);
        consumer.setMessageListener(this);
    }

    @Override
    public void onMessage(Message message) {
        System.out.println(message);
        if (message != null && message instanceof MapMessage) {
            MapMessage msg = (MapMessage) message;
            String nickName = null;
            String opinions = null;
            try {
                nickName = msg.getString("nickName");
                opinions = msg.getString("opinions");
                System.out.println(nickName);
                System.out.println(opinions);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}


五、Jfinal 插件配置

  @Override
    public void configPlugin(Plugins me) {
     me.add(new ActiveMQPlugin());
    }

六、接口调用测试

   public void testSend2() throws JMSException {
        JmsSender sender = new JmsSender(ActiveMQ.getConnection("sendMail"), ActiveMQPlugin.QUEUE_NAME);
        MapMessage mapMessage = sender.getSession().createMapMessage();
        mapMessage.setString("nickName","123");
        mapMessage.setString("opinions", "456");
        sender.sendMessage(mapMessage);
        rendJson("成功");
    }

七、结果展示

在这里插入图片描述

//解析出来的成果为 nickName值为 123,opinions值为 456
"nickName":"123"
"opinions":"456"
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值