`
dannyhz
  • 浏览: 394568 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

CopyOnWriteArrayList 的使用

 
阅读更多
引用








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();
	}
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics