activeMQ性能优化--对象池管理connection
2016-12-18 13:13
232 查看
activeMQ的某个应用场景,消费者和服务器只需建立一个长连接,而生产者的情况集中在服务器,需要对服务器端的生产者连接进行优化。
首先maven引入jar包依赖
[java]
view plain
copy
<dependency>
<groupId>org.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.9.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_1.1_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
下面是实现代码
[java]
view plain
copy
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnection;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jms.*;
public class MQProductHelper {
public static final Logger LOG = LoggerFactory.getLogger(MQProductHelper.class);
private static PooledConnectionFactory poolFactory;
/**
* 获取单例的PooledConnectionFactory
* @return
*/
private static synchronized PooledConnectionFactory getPooledConnectionFactory() {
LOG.info("getPooledConnectionFactory");
if (poolFactory != null) return poolFactory;
LOG.info("getPooledConnectionFactory create new");
IConfigService configService = ServiceManager.getService(IConfigService.class);
String userName = configService.getConfig("MQ_USER_NAME", ShopConstant.BC_SHOP_ID);
String password = configService.getConfig("MQ_USER_PASS", ShopConstant.BC_SHOP_ID);
String url = configService.getConfig("MQ_BROKER_URL", ShopConstant.BC_SHOP_ID);
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(userName, password, url);
poolFactory = new PooledConnectionFactory(factory);
// 池中借出的对象的最大数目
poolFactory.setMaxConnections(100);
poolFactory.setMaximumActiveSessionPerConnection(50);
//后台对象清理时,休眠时间超过了3000毫秒的对象为过期
poolFactory.setTimeBetweenExpirationCheckMillis(3000);
LOG.info("getPooledConnectionFactory create success");
return poolFactory;
}
/**
* 1.对象池管理connection和session,包括创建和关闭等
* 2.PooledConnectionFactory缺省设置MaxIdle为1,
* 官方解释Set max idle (not max active) since our connections always idle in the pool. *
* @return * @throws JMSException
*/
public static Session createSession() throws JMSException {
PooledConnectionFactory poolFactory = getPooledConnectionFactory();
PooledConnection pooledConnection = (PooledConnection) poolFactory.createConnection();
//false 参数表示 为非事务型消息,后面的参数表示消息的确认类型(见4.消息发出去后的确认模式)
return pooledConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
public static void produce(String subject, String msg) {
LOG.info("producer send msg: {} ", msg);
if (StringUtil.isEmpty(msg)) {
LOG.warn("发送消息不能为空。");
return;
}
try {
Session session = createSession();
LOG.info("create session");
TextMessage textMessage = session.createTextMessage(msg);
Destination destination = session.createQueue(subject);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.send(textMessage);
LOG.info("create session success");
} catch (JMSException e) {
LOG.error(e.getMessage(), e);
}
}
public static void main(String[] args) {
MQProductHelper.produce("test.subject", "hello");
}
}
首先maven引入jar包依赖
[java]
view plain
copy
<dependency>
<groupId>org.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.9.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_1.1_spec</artifactId>
</exclusion>
</exclusions>
</dependency>
下面是实现代码
[java]
view plain
copy
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.pool.PooledConnection;
import org.apache.activemq.pool.PooledConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jms.*;
public class MQProductHelper {
public static final Logger LOG = LoggerFactory.getLogger(MQProductHelper.class);
private static PooledConnectionFactory poolFactory;
/**
* 获取单例的PooledConnectionFactory
* @return
*/
private static synchronized PooledConnectionFactory getPooledConnectionFactory() {
LOG.info("getPooledConnectionFactory");
if (poolFactory != null) return poolFactory;
LOG.info("getPooledConnectionFactory create new");
IConfigService configService = ServiceManager.getService(IConfigService.class);
String userName = configService.getConfig("MQ_USER_NAME", ShopConstant.BC_SHOP_ID);
String password = configService.getConfig("MQ_USER_PASS", ShopConstant.BC_SHOP_ID);
String url = configService.getConfig("MQ_BROKER_URL", ShopConstant.BC_SHOP_ID);
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(userName, password, url);
poolFactory = new PooledConnectionFactory(factory);
// 池中借出的对象的最大数目
poolFactory.setMaxConnections(100);
poolFactory.setMaximumActiveSessionPerConnection(50);
//后台对象清理时,休眠时间超过了3000毫秒的对象为过期
poolFactory.setTimeBetweenExpirationCheckMillis(3000);
LOG.info("getPooledConnectionFactory create success");
return poolFactory;
}
/**
* 1.对象池管理connection和session,包括创建和关闭等
* 2.PooledConnectionFactory缺省设置MaxIdle为1,
* 官方解释Set max idle (not max active) since our connections always idle in the pool. *
* @return * @throws JMSException
*/
public static Session createSession() throws JMSException {
PooledConnectionFactory poolFactory = getPooledConnectionFactory();
PooledConnection pooledConnection = (PooledConnection) poolFactory.createConnection();
//false 参数表示 为非事务型消息,后面的参数表示消息的确认类型(见4.消息发出去后的确认模式)
return pooledConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
public static void produce(String subject, String msg) {
LOG.info("producer send msg: {} ", msg);
if (StringUtil.isEmpty(msg)) {
LOG.warn("发送消息不能为空。");
return;
}
try {
Session session = createSession();
LOG.info("create session");
TextMessage textMessage = session.createTextMessage(msg);
Destination destination = session.createQueue(subject);
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.send(textMessage);
LOG.info("create session success");
} catch (JMSException e) {
LOG.error(e.getMessage(), e);
}
}
public static void main(String[] args) {
MQProductHelper.produce("test.subject", "hello");
}
}
相关文章推荐
- Windows Server 2003网络配置与管理 优化服务器的性能
- cacti性能优化笔记(面向大规模集群的监控管理)
- ActiveMq性能优化
- linux 性能 管理 与 优化
- android,性能优化,内存优化管理,高级缓存 (转)
- JBossAS性能优化和瘦身系列(6):Deployment Scanner & Connection Pools
- web页面内容优化管理与性能技巧
- android,性能优化,内存优化管理,高级缓存
- android,性能优化,内存优化管理,高级缓存
- web页面内容优化管理与性能技巧
- 【原】SqlServer性能优化——Partition(管理分区)
- 一起谈.NET技术,Sql Server性能优化——Partition(管理分区)
- Sql Server性能优化——Partition(管理分区)
- 【Hibernate】性能优化之缓存管理
- android,性能优化,内存优化管理,高级缓存
- Windows2008管理---第8章 监视和优化性能
- SQL Sentry:专业化的数据库性能优化建议和事件管理软件
- Hibernate的cache管理,性能优化(大数据量)
- 【性能优化】optimizer statistics统计信息管理技巧
- web页面内容优化管理与性能技巧