CopyOnWriteArrayList 的使用

[quote]


[/quote]




package com.statestr.gcth.ext.jms.oracleaq;

import java.sql.SQLException;
import java.util.Properties;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.jms.JMSException;

import oracle.jdbc.pool.OracleConnectionPoolDataSource;
import oracle.jms.AQjmsFactory;

import org.apache.log4j.Logger;

import com.statestr.gcth.core.InitializationException;
import com.statestr.gcth.core.annotation.InjectComponent;
import com.statestr.gcth.core.annotation.InjectProperty;
import com.statestr.gcth.core.configuration.model.ComponentConfig;
import com.statestr.gcth.core.context.ContextManager;
import com.statestr.gcth.core.jms.AbstractJmsConnectionResource;
import com.statestr.gcth.core.jms.JmsConnection;
import com.statestr.gcth.core.jms.JmsEndpointException;
import com.statestr.gcth.core.security.SecurityHandler;
import com.statestr.gcth.core.util.CommonUtil;

/**
* Connection factory for ActiveMQ.
*
* @author Jimmy Zhang
*/
public class OracleAqJmsConnectionResource extends
AbstractJmsConnectionResource
{
private static final Logger LOGGER = Logger
.getLogger(OracleAqJmsConnectionResource.class);

@InjectProperty(required = true)
private String brokerUrl;

@InjectProperty(required = true)
private String user;

@InjectProperty(required = false)
private String password;

@InjectProperty(required = false)
private String keyHost;

private OracleConnectionPoolDataSource dataSource = null;

@InjectComponent(required = false)
SecurityHandler securityHandler;


@InjectProperty(required = false)
private Long readTimeout = 0L; // 0 default disable

@InjectProperty(required = false)
private Boolean oracleNetDisableOob = true;

@InjectProperty(required = false)
private String systemProperties = null;

/**
* {@inheritDoc}
*/
@Override
public void config(ComponentConfig config)
{
super.config(config);

// use the security handler to retrieve password
// if it is available.
if (securityHandler == null) {
securityHandler = ContextManager.getSecurityHandler();
}
if (securityHandler != null) {
password = securityHandler.retrievePassword(keyHost, user);
}

try {
dataSource = new OracleConnectionPoolDataSource();
Properties properties = CommonUtil.retrieveStringProperties(systemProperties);;
// process id as current pid
properties.setProperty("oracle.jdbc.v$session.process", String.valueOf(CommonUtil.getPid()));
properties.setProperty("oracle.jdbc.ReadTimeout", String.valueOf(readTimeout * 1000));
properties.setProperty("oracle.net.disableOob", String.valueOf(oracleNetDisableOob));
dataSource.setConnectionProperties(properties);

//dataSource.setDriverClass("oracle.jdbc.OracleDriver");
dataSource.setURL(brokerUrl);
dataSource.setUser(user);
dataSource.setPassword(password);

// the settings below are optional -- c3p0 can work with defaults
//dataSource.setMinPoolSize(initialPoolSize);
//dataSource.setMaxPoolSize(maxPoolSize);
//dataSource.setAcquireIncrement(this.aquireIncrement);

connectionFactory = AQjmsFactory.getConnectionFactory(dataSource);
connections = new CopyOnWriteArrayList<JmsConnection>();
createConnections(this.initialPoolSize);
LOGGER.debug(String.format(
"connection pool initialized with %d connections",
initialPoolSize));
}
catch (JMSException e) {
String error = "Failed to get connectionFactory with error "
+ e.getMessage();
LOGGER.error(error);
throw new InitializationException(error);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LOGGER.debug(String.format(
"connection pool initialized with %d connections",
initialPoolSize));
// replace cxCode for OracleAQ
if (DEFAULT_JMS_CX_CODE.equals(cxCode)) {
cxCode = "CX610";
}
}



@Override
protected void createConnections(int connCout)
{
for (int i = 0; i < connCout; i++) {
try {
if ( connections.size() >= maxPoolSize) {
return;
}
JmsConnection conn = new JmsConnection(this, connectionFactory.createConnection());
conn.start();
conn.open();
connections.add(conn);
LOGGER.info("create the JmsConnection " + conn);
}

catch (JMSException e) {
String error = cxCode + " Unable to create AQ JMS connection with error "
+ e.getMessage();
LOGGER.fatal(error, e);
throw new JmsEndpointException("failed to create connections",
e);
}
}
}

@Override
public String toString()
{
StringBuilder builder = new StringBuilder(super.toString());

builder.append(",brokerUrl=").append(brokerUrl);
builder.append(",user=").append(user);

return builder.toString();
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值