原创:jfinal2.0整合activemq

阅读更多

     最近项目中需要用到jfinal整合activemq,请教了下度娘,jfinal没有出这个插件,所以自己小小的封装了一下,跑起来居然可以用,在这里把源码分享给大家!不喜勿喷,希望给我提一些意见!

     github地址: https://github.com/javaxiaoyetan/istudy.git

   

1.项目目录截图

             原创:jfinal2.0整合activemq_第1张图片

         

 2.pom文件添加依赖

   



    4.0.0

    com.xiaoyetanmodules
    jfinalAvtivemq
    war
    1.0

    
        UTF-8
        1.8
    

    
        
        
            jfinal
            jfinal
            2.0
            system
            ${basedir}/src/main/webapp/WEB-INF/lib/jfinal-2.0-bin-with-src.jar
        
        
            com.jfinal
            cos
            26Dec2008
        

        
        
            org.slf4j
            slf4j-api
            1.7.10
        
        
            ch.qos.logback
            logback-classic
            1.1.2
        
        
            ch.qos.logback
            logback-core
            1.1.2
        
        
        
            org.apache.activemq
            activemq-all
            5.13.2
        
        
            org.apache.commons
            commons-lang3
            3.0
        
        
        
            org.apache.commons
            commons-pool2
            2.1
        

        
            com.alibaba
            fastjson
            1.1.15
        
    


 

 3.ActivemqPlugin 

package com.xiaoyetan.common.mq.plugins;

import java.util.HashMap;
import java.util.Map;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.apache.activemq.thread.TaskRunnerFactory;
import com.jfinal.plugin.IPlugin;
import org.apache.log4j.Logger;

import javax.jms.*;

/**
 * @Author xiaoyetan
 * @Date :created on 17:26 2017/8/23
 */
public class ActivemqPlugin implements IPlugin{
    private final static Logger log = Logger.getLogger(ActivemqPlugin.class);
    private String brokerUrl;

    private ActiveMQConnectionFactory targetFactory;

    private PooledConnectionFactory pooledConFactory;

    private Connection connection;

    private Session session;

    private Map queueMap = new HashMap();
    private boolean isStarted = false;


    private Map queueProducer = new HashMap();

    public ActivemqPlugin(String brokerUrl) {
        this.brokerUrl = brokerUrl;
        try {
            init();
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }


    public void init() throws JMSException {
        if (null == pooledConFactory) {
            targetFactory = new ActiveMQConnectionFactory(brokerUrl);
            targetFactory.setUseAsyncSend(true);
            TaskRunnerFactory taskRunnerFactory = new TaskRunnerFactory();
            taskRunnerFactory.setMaxIterationsPerRun(2);
            taskRunnerFactory.setMaxThreadPoolSize(10);
            taskRunnerFactory.setShutdownAwaitTermination(10);
            taskRunnerFactory.setDaemon(false);
            targetFactory.setSessionTaskRunner(taskRunnerFactory);
            //pooledConFactory = new PooledConnectionFactory(brokerUrl);  //都可以
            pooledConFactory = new PooledConnectionFactory((ActiveMQConnectionFactory) targetFactory);
            pooledConFactory.setMaxConnections(200);
        }
    }

    public Session createCon() throws JMSException {
        if (null == connection) {
            connection = pooledConFactory.createConnection();
            connection.start();
        }
        if (null == session) {
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        }
        isStarted =true;
        return session;
    }

    public void addQueue(String queueName) {
        if (null == queueName || queueName.trim().equals("")) {
            throw new RuntimeException("Queue name parameter is blank!");
        }
        synchronized (queueMap) {
            if (null == queueMap) {
                queueMap = new HashMap();
            }
            if (!queueMap.containsKey(queueName)) {
                queueMap.put(queueName, new ActiveMQQueue(queueName));
            }
        }
    }

    public void addQueneMessageListener(String queneName, MessageListener msgListenre) throws JMSException {
        addQueue(queneName);
        MessageConsumer comsumer = createCon().createConsumer(queueMap.get(queneName));
        comsumer.setMessageListener(msgListenre);
    }


    public void sendQueueMsg(String queueName, String msg) throws JMSException {
        addQueue(queueName);
        //创建一个生产者,然后发送多个消息。
        if (null == queueProducer) {
            queueProducer = new HashMap();
        }
        if (!queueProducer.containsKey(queueName)) {
            queueProducer.put(queueName, createCon().createProducer(queueMap.get(queueName)));
        }
        MessageProducer producer = queueProducer.get(queueName);
        //producer.setTimeToLive(time);
        producer.send(createCon().createTextMessage(msg));
    }

    public void setBrokerUrl(String brokerUrl) {
        this.brokerUrl = brokerUrl;
    }

    public String getBrokerUrl() {
        return brokerUrl;
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public boolean stop() {
        return false;
    }

    public boolean start() {
        if (null != session) {
            log.info("activemq isStarted>>>");
            return true;
        }else{
            new ActivemqService();
        }
        return true;
    }

}

 

   4.ActivemqService 

package com.xiaoyetan.common.mq.plugins;

import com.xiaoyetan.Const;
import com.xiaoyetan.common.mq.CallCrmUnsubListener;
import org.apache.log4j.Logger;

import javax.jms.JMSException;


/**
 * @Author xiaoyetan
 * @Date :created on 17:28 2017/8/23
 */
public class ActivemqService {

    private final static Logger log = Logger.getLogger(ActivemqService.class);

    private static  String brokerUrl = Const.getValue("mq.brokerURL");
    public static final String CrmUnsubQueueName=Const.getValue("crm.unsub.queue");

    private static ActivemqPlugin mq ;

    static{
        try {
            log.info("ActivemqService static >>>");
            mq= new ActivemqPlugin(brokerUrl);
            mq.addQueneMessageListener(CrmUnsubQueueName, new CallCrmUnsubListener());
            log.info("ActivemqService static end>>>");
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取MQ工具类
     * @return
     */
    public static ActivemqPlugin MQ(){
        return mq;
    }

    // 发送消息
    public static void sendMessage(String queueName, final String msgJSON) {
        try {
            mq.sendQueueMsg(queueName,msgJSON);
            log.info(msgJSON);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}

5.jinalCoreConfig

package com.xiaoyetan.jfinal;

import com.jfinal.config.*;
import com.jfinal.kit.PropKit;
import com.xiaoyetan.Const;
import com.xiaoyetan.common.mq.plugins.ActivemqPlugin;

/**
 * @Author xiaoyetan
 * @Date :created on 17:22 2017/8/23
 */
public class JfinalCoreConfig extends JFinalConfig {
    //配置常量
    public void configConstant(Constants me) {
        PropKit.use("config.properties");
    }

    //配置路由
    public void configRoute(Routes me) {
    }

    //配置插件
    public void configPlugin(Plugins me) {
        ActivemqPlugin mqPlugin = new ActivemqPlugin(Const.getValue("mq.brokerURL"));
        me.add(mqPlugin);
    }

    //配置拦截器
    public void configInterceptor(Interceptors me) {

    }

    //配置处理器
    public void configHandler(Handlers me) {
    }

}

 

         

  • 原创:jfinal2.0整合activemq_第2张图片
  • 大小: 37.8 KB
  • 查看图片附件

你可能感兴趣的:(jfinal,activemq)