有两种连接方式

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池的更多相关文章

  1. ActiveMQ学习心得:连接池的简单实现和模板模式的应用

    一.安装activemq 下载地址:https://archive.apache.org/dist/activemq/5.13.0/apache-activemq-5.13.0-bin.zip 下载完 ...

  2. ActiveMQ 的线程池

    ActiveMQ 的线程池实质上也是 ThreadPoolExecutor,不过它的任务模型有自己的特点,我们先看一个例子: public static void main(String[] args ...

  3. JMS之——ActiveMQ时抛出的错误Could not connect to broker URL-使用线程池解决高并发连接

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/69046395 解决使用activemq时抛出的异常:javax.j ms.JMSE ...

  4. ActiveMQ使用线程池实现消息的生产与消费

    jar文件:spring3.1jar,以及 项目src路径下文件:config.properties 读取config.properties文件JAVA类: package com.lejob.lej ...

  5. JMS学习之路(一):整合activeMQ到SpringMVC

    JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到实际的业务需求中的话我们可以 ...

  6. Spring下ActiveMQ实战

    MessageQueue是分布式的系统里经常要用到的组件,一般来说,当需要把消息跨网段.跨集群的分发出去,就可以用这个.一些典型的示例就是: 1.集群A中的消息需要发送给多个机器共享: 2.集群A中消 ...

  7. ActiveMQ与spring集成实现Queue模式

    ActiveMQ可以和spring很好的集成,下面我们来看看,如何做个集成的demo. (1)pom.xml引入相关jar <!-- spring相关 begin --> <depe ...

  8. ActiveMQ的介绍及使用实例.

    今天就来说下 这个项目中使用ActiveMQ的情况, MQ: message queue, 顾名思义就是消息队列的意思. 一: 使用场景:  消息队列在大型电子商务类网站,如京东.淘宝.去哪儿等网站有 ...

  9. ActiveMQ 即时通讯服务 浅析

      一. 概述与介绍 ActiveMQ 是Apache出品,最流行的.功能强大的即时通讯和集成模式的开源服务器.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provi ...

随机推荐

  1. 用gulp替代fekit构建前端项目

    https://segmentfault.com/a/1190000003060016 离开qunar有一个多月了,在离开的时候就决定不再用fekit.做出这个决定并不是因为fekit不好,恰恰相反, ...

  2. Java的集合类

    转自:Q.Mr的博客 http://www.cnblogs.com/zrtqsk/p/3472258.html (本人第一次写博客,部分内容有参照李刚老师的疯狂java系列图书,如有遗漏错误,请多指教 ...

  3. jquery实现隐藏,简化和更多

    HTML代码: <div class="box"> <div class="header"> <h3>图书分类</h3 ...

  4. 【HDU】1517 A Multiplication Game

    http://acm.hdu.edu.cn/showproblem.php?pid=1517 题意:每次乘上2~9..p>=n时赢.. #include <cstdio> #incl ...

  5. Android -- 重设字符并统计原字符以及修改字符的长度以及位置

    1. 效果图

  6. BZOJ2453维护队列&&BZOJ2120数颜色

    2016-05-28 11:20:22 共同的思路: 维护某种颜色上一次在哪里出现pre,可以知道当pre<询问的l时更新答案 块内按照pre排序 修改的时候重新O(n)扫一遍,如果和之前的不一 ...

  7. 直接用Qt写soap

    直接用Qt写soap 最近的项目里用到了webservice, 同事用的是`gSoap`来搞的. 用这个本身没什么问题, 但这货生成的代码实非人类可读, 到处都是`__`和`_`, 看得我眼晕.... ...

  8. XAML parser exception

    To debug xaml exception: Visual Studio -> Debug -> Exception -> Common Language Runtime Exc ...

  9. [LintCode] House Robber II 打家劫舍之二

    After robbing those houses on that street, the thief has found himself a new place for his thievery ...

  10. SQL中的with as

    一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到.有的时候,是为了让 ...