有两种连接方式

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. HDU 1072(记忆化BFS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...

  2. ubuntu 安装 Terminator

    sudo apt-get install terminator Ctrl-Shift-E: 垂直分割Ctrl-Shift-O: 水平分割Ctrl-Shift-P: 激活先前的窗口Ctrl-Shift- ...

  3. ThinkPHP3.2.2 Widget扩展以及widget demo实例

    Widget扩展一般用于页面组件的扩展. 先说明Widget被调用的方法,你只需要在你的模板文件中使用这样的语法:{:W("Demo/demo_widget_method",arr ...

  4. 深入浅出 - Android系统移植与平台开发(八)- HAL Stub框架分析

    作者:唐老师,华清远见嵌入式学院讲师. 1. HAL Stub框架分析 HAL stub的框架比较简单,三个结构体.两个常量.一个函数,简称321架构,它的定义在:@hardware/libhardw ...

  5. 使用diff制作补丁

    1.制作补丁包 命令格式 diff -uNr  oldfile.c newfile.c > x.patch 2.打补丁 命令格式 patch -p0 < x.patch 总结一下:单个文件 ...

  6. java读取邮件

    package com.zz.mail; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...

  7. 用atom写LaTeX文档

    下载并安装Tex Live: 下载页面 下载并安装atom:下载页面 打开atom File -> Settings -> Install 搜索并安装: language-latex la ...

  8. sendfile传输机制

  9. iptables防火墙作为基本需求的配置

    企业中使用iptables防火墙:(一般不要在命令中输入规则) # Firewall configuration written by system-config-firewall# Manual c ...

  10. org.hibernate.hql.ast.QuerySyntaxException: XXX is not mapped

    因为 String sql2 = "select s from Student s where s.clazz.name=:name"; 此处的 Student 应该为类名.hql ...