package com.xiaoyeta.config;
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 com.xiaoyetan.sender.ActivemqPubclient;
import org.apache.log4j.Logger;
import javax.jms.*;
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 start() {
// TODO Auto-generated method stub
return false;
}
public boolean stop() {
// TODO Auto-generated method stub
return false;
}
/* public boolean stop() {
return false;
}
*/
/*public boolean start() {
if (null != session) {
log.info("activemq isStarted>>>");
return true;
}else{
new ActivemqPubclient();
}
return true;
}*/
}
没有合适的资源?快使用搜索试试~ 我知道了~
jfianl整合activemq
共34个文件
class:7个
java:6个
xml:4个
需积分: 10 6 下载量 194 浏览量
2018-09-06
09:24:08
上传
评论
收藏 2.19MB ZIP 举报
温馨提示
jfianl整合activemq
资源推荐
资源详情
资源评论
收起资源包目录
jfinalAvtivemq1.1.0.zip (34个子文件)
istudy-master
jfinalActivemq.iml 2KB
pom.xml 2KB
target
test-classes
m2e-wtp
web-resources
META-INF
maven
com.xiaoyetanmodules
jfinalAvtivemq
pom.xml 2KB
pom.properties 240B
MANIFEST.MF 108B
classes
com
xiaoyetan
sender
ActivemqPubclient.class 2KB
test
Pubclient.class 1KB
listener
UCListener.class 1KB
UCListener$1.class 2KB
xiaoyeta
config
User.class 764B
JfinalCoreConfig.class 2KB
ActivemqPlugin.class 5KB
config.properties 125B
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.core.resources.prefs 124B
org.eclipse.wst.common.component 687B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 50B
.jsdtscope 639B
org.eclipse.jdt.core.prefs 430B
src
main
webapp
WEB-INF
web.xml 718B
lib
ojdbc14.jar 1.47MB
jfinal-2.0-bin-with-src.jar 861KB
resources
config.properties 125B
java
com
xiaoyetan
sender
ActivemqPubclient.java 1KB
test
Pubclient.java 490B
listener
UCListener.java 1KB
xiaoyeta
config
JfinalCoreConfig.java 987B
User.java 481B
ActivemqPlugin.java 5KB
.project 1KB
.classpath 1KB
共 34 条
- 1
资源评论
王树民
- 粉丝: 904
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功