最近项目中需要用到jfinal整合activemq,请教了下度娘,jfinal没有出这个插件,所以自己小小的封装了一下,跑起来居然可以用,在这里把源码分享给大家!不喜勿喷,希望给我提一些意见!
github地址: https://github.com/javaxiaoyetan/istudy.git
1.项目目录截图
2.pom文件添加依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xiaoyetanmodules</groupId> <artifactId>jfinalAvtivemq</artifactId> <packaging>war</packaging> <version>1.0</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jdk.version>1.8</jdk.version> </properties> <dependencies> <!-- jfinal --> <dependency> <groupId>jfinal</groupId> <artifactId>jfinal</artifactId> <version>2.0</version> <scope>system</scope> <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/jfinal-2.0-bin-with-src.jar</systemPath> </dependency> <dependency> <groupId>com.jfinal</groupId> <artifactId>cos</artifactId> <version>26Dec2008</version> </dependency> <!-- logback --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.10</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.2</version> </dependency> <!-- mq --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.13.2</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.15</version> </dependency> </dependencies> </project>
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<String, Queue> queueMap = new HashMap<String, Queue>(); private boolean isStarted = false; private Map<String, MessageProducer> queueProducer = new HashMap<String, MessageProducer>(); 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<String, Queue>(); } 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<String, MessageProducer>(); } 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整合Apache Shiro权限框架详解** JFinal是一个基于Java的高效、轻量级的Web开发框架,它的设计目标是让开发者能够快速地进行开发,减少了大量重复的代码工作。Apache Shiro则是一款强大的安全管理框架,...
### JFinal 2.0 中文手册知识点梳理 #### 第0章 极速升级至2.0.6 - **AOP升级**:在2.0版本中,`Interceptor`接口中的`ActionInvocation`参数被重命名为`Invocation`。这意味着在自定义拦截器时,需要更新相关代码...
《JFinal 2.0全集:一次深入的探索与解析》 JFinal,作为国内开发者广泛使用的Java Web开发框架,以其轻量级、高效、易用的特点深受喜爱。本次我们聚焦于JFinal 2.0版本,通过解构"jfinal-2.0-all.zip"这个压缩包,...
《JFinal 2.0:Java Web开发的强大框架》 JFinal是基于Java语言的轻量级Web开发框架,它的核心设计目标是开发迅速、代码量少、学习简单、性能优秀、足够API等特点,使得它在Java Web开发领域内备受青睐。本资源...
**基于JFinal 2.0开发的Java版商城系统详解** **一、JFinal简介** JFinal 是一个基于 Java 语言的轻量级 Web 开发框架,它采用 MVC(Model-View-Controller)架构模式,旨在简化 Java Web 开发过程。JFinal 的核心...
基于 JFinal2.0 + Bootstrap + SeaJS + FullC的终极排班管理系统对人员信息的维护,增删改查等,对人员进行分组,对不同的任务或者部门人员分组来管理排班,分组支持增删改查等,排班班次设置,支持自定义班次名称、...
Jfinal是一款高效的Java Web开发框架,它以其快速开发、代码量少、学习曲线平缓、功能强大和轻量级的特点深受开发者喜爱。在本文中,我们将深入探讨Jfinal的核心特性、设计理念以及如何使用它进行实际项目开发。 ##...
赠送jar包:jfinal-3.0.jar; 赠送原API文档:jfinal-3.0-javadoc.jar; 赠送源代码:jfinal-3.0-sources.jar; 赠送Maven依赖信息文件:jfinal-3.0.pom; 包含翻译后的API文档:jfinal-3.0-javadoc-API文档-中文...
赠送jar包:jfinal-3.0.jar; 赠送原API文档:jfinal-3.0-javadoc.jar; 赠送源代码:jfinal-3.0-sources.jar; 赠送Maven依赖信息文件:jfinal-3.0.pom; 包含翻译后的API文档:jfinal-3.0-javadoc-API文档-中文...
基于JFinal 2.0加入一些kit,它们有 扩展JFinalConfig=> JFinalConfigExt 1.1 给每一个app设置一个name; 1.2 从配置文件中获取文件的保存路径; 1.3 获取devmode; 1.4 打包DruidPlugin和ActiveRecordPlugin; 以上...
jfinal2.0 官方最新版本,包括源码,文档手册。
赠送jar包:jfinal-mail-plugin-3.0.jar; 赠送原API文档:jfinal-mail-plugin-3.0-javadoc.jar; 赠送源代码:jfinal-mail-plugin-3.0-sources.jar; 赠送Maven依赖信息文件:jfinal-mail-plugin-3.0.pom; 包含...
赠送jar包:JFinal-ueditor-0.0.3.jar; 赠送原API文档:JFinal-ueditor-0.0.3-javadoc.jar; 赠送源代码:JFinal-ueditor-0.0.3-sources.jar; 赠送Maven依赖信息文件:JFinal-ueditor-0.0.3.pom; 包含翻译后的API...
赠送jar包:JFinal-ueditor-0.0.3.jar; 赠送原API文档:JFinal-ueditor-0.0.3-javadoc.jar; 赠送源代码:JFinal-ueditor-0.0.3-sources.jar; 赠送Maven依赖信息文件:JFinal-ueditor-0.0.3.pom; 包含翻译后的API...
赠送jar包:jfinal-mail-plugin-3.0.jar; 赠送原API文档:jfinal-mail-plugin-3.0-javadoc.jar; 赠送源代码:jfinal-mail-plugin-3.0-sources.jar; 赠送Maven依赖信息文件:jfinal-mail-plugin-3.0.pom; 包含...
jfinal-oauth2.0-server基于,参考实现了4.节描述的内容。实现了OAuth 2.0定义了四种授权方式授权码模式(authorization code): 先获取下次请求token的code,然后在带着code去请求token;简化模式(implicit):...
jfinal-oauth2.0-server jfinal-oauth2.0-server 基于,, 参考 实现了4.节描述的内容。 实现了OAuth 2.0定义了四种授权方式 授权码模式(authorization code): 先获取下次请求token的code,然后在带着code去请求...
《JFinal开发实例详解——快速入门指南》 JFinal,作为国内最受欢迎的Java Web MVC框架之一,以其轻量级、高效能的特点深受开发者喜爱。本文将通过“jfinaldemo”这个实例,帮助你深入理解JFinal框架的开发步骤,让...
jfinal 2.2 开发包 + 源码 的整合,方便非 maven 开发者在开发时调试和查看源码,从而不用再单独绑定 jfinal-2.2-src.zip 就可以在 IDE 中查看源码 4:jetty-server-8.1.8.jar jetty-server-8.1.8.jar 用来支持...
JAVA语言JFinal框架教程 JAVA语言的MVC架构,你只会使用structs和Spring MVC吗?来学学JFinal吧