ActiveMq池
有两种连接方式
1.Spring 引用
<!-- 配置JMS连接工厂 -->
<bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>failover:(tcp://192.168.2.211:62617,tcp://192.168.2.211:62618,tcp://192.168.2.211:62619)?maxReconnectAttempts=10</value>
</property>
<property name="useAsyncSend">
<value>true</value>
</property>
</bean>
</property>
</bean>
2.配置文件
MqUserName=admin
MqPassword=admin
MqUrl=failover:(tcp://192.168.2.211:62617,tcp://192.168.2.211:62618,tcp://192.168.2.211:62619)
3.代码
import java.util.Properties; import javax.jms.Connection;
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.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class ActiveMqUtils { private static PooledConnectionFactory factory; static{
//方式一
// Properties pps = new Properties();
// String url="",name="",pwd="";
// try {
// pps.load(ActiveMqUtils.class.getClassLoader().getResourceAsStream("MqProperty.properties"));
// url = pps.getProperty("MqUrl");
// name = pps.getProperty("MqUserName");
// pwd = pps.getProperty("MqPassword");
// ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
// activeMQConnectionFactory.setUserName(name);
// activeMQConnectionFactory.setPassword(pwd);
// activeMQConnectionFactory.setBrokerURL(url);
//
// factory = new PooledConnectionFactory( activeMQConnectionFactory); // session数
// int maximumActive = 5;
// factory.setMaximumActiveSessionPerConnection(maximumActive);
// factory.setIdleTimeout(120);
// factory.setMaxConnections(5);
// factory.setBlockIfSessionPoolIsFull(true);
// } catch (Exception e) {
// e.printStackTrace();
// } //方式二
String[] springConfigFiles = {"mybatis-spring-config.xml","module-service-config.xml" };
ApplicationContext ctx = new ClassPathXmlApplicationContext( springConfigFiles );
factory = (PooledConnectionFactory) ctx.getBean( "connectionFactory" );
} public static void doSend(String key,String message){
Connection connection =null;
Session session;
Destination destination;
MessageProducer producer;
try {
connection =factory.createConnection();
connection.start();
session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(key);
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
TextMessage msgObj = session .createTextMessage(message);
producer.send(msgObj);
session.commit();
}catch (Exception e) {
e.printStackTrace();
}
finally {
close(connection);
}
} public static String doReceive(String key){
String rs="";
Connection connection =null;
Session session;
Destination destination;
MessageConsumer consumer;
try {
connection =factory.createConnection();
connection.start();
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("sales");
consumer = session.createConsumer(destination);
TextMessage message = (TextMessage) consumer.receive(10);
if (null != message) {
rs = message.getText();
}
}catch (Exception e) {
e.printStackTrace();
}
finally {
close(connection);
}
return rs;
} /**
* 关闭连接
*/
public static void close(Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (JMSException e) { e.printStackTrace();
}
} public static void main(String[] agrs){ for(int i= 0;i<20;i++){
long begin = System.currentTimeMillis();
ActiveMqUtils.doSend("1111", "sss");
long end = System.currentTimeMillis();
System.out.println("耗时:"+(end-begin));
} }
}
ActiveMq池的更多相关文章
- ActiveMQ学习心得:连接池的简单实现和模板模式的应用
一.安装activemq 下载地址:https://archive.apache.org/dist/activemq/5.13.0/apache-activemq-5.13.0-bin.zip 下载完 ...
- ActiveMQ 的线程池
ActiveMQ 的线程池实质上也是 ThreadPoolExecutor,不过它的任务模型有自己的特点,我们先看一个例子: public static void main(String[] args ...
- JMS之——ActiveMQ时抛出的错误Could not connect to broker URL-使用线程池解决高并发连接
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/69046395 解决使用activemq时抛出的异常:javax.j ms.JMSE ...
- ActiveMQ使用线程池实现消息的生产与消费
jar文件:spring3.1jar,以及 项目src路径下文件:config.properties 读取config.properties文件JAVA类: package com.lejob.lej ...
- JMS学习之路(一):整合activeMQ到SpringMVC
JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到实际的业务需求中的话我们可以 ...
- Spring下ActiveMQ实战
MessageQueue是分布式的系统里经常要用到的组件,一般来说,当需要把消息跨网段.跨集群的分发出去,就可以用这个.一些典型的示例就是: 1.集群A中的消息需要发送给多个机器共享: 2.集群A中消 ...
- ActiveMQ与spring集成实现Queue模式
ActiveMQ可以和spring很好的集成,下面我们来看看,如何做个集成的demo. (1)pom.xml引入相关jar <!-- spring相关 begin --> <depe ...
- ActiveMQ的介绍及使用实例.
今天就来说下 这个项目中使用ActiveMQ的情况, MQ: message queue, 顾名思义就是消息队列的意思. 一: 使用场景: 消息队列在大型电子商务类网站,如京东.淘宝.去哪儿等网站有 ...
- ActiveMQ 即时通讯服务 浅析
一. 概述与介绍 ActiveMQ 是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provi ...
随机推荐
- BZOJ3553 : [Shoi2014]三叉神经树
设val[i]为i连出去的树突中输出值为0的个数 如果val[x]<=1,输出值为1,否则输出值为0 修改x就相当于val[f[i]]++或者val[f[i]]-- 用Link-cut Tree ...
- 自动备份mysql
创建备份文件存放的目录 mkdir /usr/local/dbbak 脚本:vi /usr/local/mysqlback.sh # /bin/bash DB_NAME="dsideal_d ...
- [ACM训练] DEV C++如何处理不能调试
复试上机一定要用DEV C++,使用不熟练,出现一些问题,记录如下: 1.DEV C++ 不能调试 (1)tools->compiler option->settings:linker里设 ...
- [深入浅出WP8.1(Runtime)]文本框(TextBox)
4.4 文本框(TextBox) 文本框(TextBox)控件是表示一个可用于显示和编辑单格式.多行文本的控件.TextBox控件常用于在表单中编辑非格式化文本,例如,如果一个表单要求输入用户姓名.电 ...
- [BZOJ2792][Poi2012]Well
2792: [Poi2012]Well Time Limit: 40 Sec Memory Limit: 64 MBSubmit: 137 Solved: 61[Submit][Status][D ...
- URAL 1031. Railway Tickets(spfa)
题目链接 不知为何会在dp里呢...INF取小了,2Y. #include <cstring> #include <cstdio> #include <string> ...
- vs2013单元测试第二部分
上次的随笔说还没弄懂,现在已经弄懂,就让我说说我的方法吧. 1.点击文件——>新建——>项目——>c#——>控制台应用程序,确定,之后如图所示 2.在一定位置写上要进行单元检测 ...
- java画图程序_图片用字母画出来
最近在研究怎样将图片用字母在文本编辑工具中“画”出来. 你看了这个可能还不知道我想说什么? 我想直接上图,大家一定就知道了 第一张:小猫 原图:http://www.cnblogs.com/hongt ...
- [LintCode] House Robber III 打家劫舍之三
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- [zt]摄像机标定(Camera calibration)笔记
http://www.cnblogs.com/mfryf/archive/2012/03/31/2426324.html 一 作用建立3D到2D的映射关系,一旦标定后,对于一个摄像机内部参数K(光心焦 ...