org.ow2.petals.bc.jms.su.JmsServiceUnitManager Maven / Gradle / Ivy
/**
* Copyright (c) 2005-2012 EBM WebSourcing, 2012-2016 Linagora
*
* This program/library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or (at your
* option) any later version.
*
* This program/library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program/library; If not, see http://www.gnu.org/licenses/
* for the GNU Lesser General Public License version 2.1.
*/
package org.ow2.petals.bc.jms.su;
import static org.ow2.petals.bc.jms.Constants.Extensions.MAX_ACTIVE;
import static org.ow2.petals.bc.jms.Constants.Extensions.MAX_IDLE;
import static org.ow2.petals.bc.jms.Constants.Extensions.MAX_WAIT;
import static org.ow2.petals.bc.jms.Constants.Extensions.MIN_EVICTABLE_IDLE_TIME_MILLIS;
import static org.ow2.petals.bc.jms.Constants.Extensions.TEST_WHILE_IDLE;
import static org.ow2.petals.bc.jms.Constants.Extensions.TIME_BETWEEN_EVICTION_RUNS_MILLIS;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.ow2.petals.bc.jms.JmsBC;
import org.ow2.petals.bc.jms.connection.JMSProviderConnection;
import org.ow2.petals.bc.jms.connection.JMSProviderConnectionFactory;
import org.ow2.petals.bc.jms.listener.JMSExternalListener;
import org.ow2.petals.component.framework.api.configuration.SuConfigurationParameters;
import org.ow2.petals.component.framework.api.exception.PEtALSCDKException;
import org.ow2.petals.component.framework.bc.BindingComponentServiceUnitManager;
import org.ow2.petals.component.framework.jbidescriptor.generated.Provides;
import org.ow2.petals.component.framework.listener.AbstractExternalListener;
import org.ow2.petals.component.framework.su.ServiceUnitDataHandler;
/**
* @author Roland Naudin - EBM WebSourcing
* @author Oliver Fabre - EBM WebSourcing
*/
public class JmsServiceUnitManager extends BindingComponentServiceUnitManager {
private final JmsBC component;
public JmsServiceUnitManager(JmsBC component) {
super(component);
this.component = component;
}
@Override
protected AbstractExternalListener createExternalListener() {
return new JMSExternalListener();
}
@Override
public void doDeploy(final ServiceUnitDataHandler suDH) throws PEtALSCDKException {
for (final Provides provides : suDH.getDescriptor().getServices().getProvides()) {
try {
this.registerConnectionPool(provides, suDH.getConfigurationExtensions(provides));
} catch (final PEtALSCDKException e) {
throw new PEtALSCDKException("Error creating a connection pool for SU: " + suDH.getName(), e);
}
}
}
@Override
public void doUndeploy(final ServiceUnitDataHandler suDH) throws PEtALSCDKException {
final PEtALSCDKException ex = new PEtALSCDKException("Error during undeploy");
for (final Provides provides : suDH.getDescriptor().getServices().getProvides()) {
try {
this.unregisterConnectionPool(provides);
} catch (final PEtALSCDKException e) {
ex.addSuppressed(e);
}
}
ex.throwIfNeeded();
}
/**
* Register a pool of JMS connections.
*
* @param provides
* @param suConfigurationParameters
* @throws PEtALSCDKException
*/
private void registerConnectionPool(final Provides provides, final SuConfigurationParameters extensions)
throws PEtALSCDKException {
final int maxActive = Integer.parseInt(extensions.get(MAX_ACTIVE, "10"));
final long maxWait = Long.parseLong(extensions.get(MAX_WAIT, "10000"));
final int maxIdle = Integer.parseInt(extensions.get(MAX_IDLE, "5"));
final long timeBetweenEvictionTimeMillis = Long.parseLong(extensions.get(TIME_BETWEEN_EVICTION_RUNS_MILLIS,
"30000"));
final long minEvictableIdleTimeMillis = Long
.parseLong(extensions.get(MIN_EVICTABLE_IDLE_TIME_MILLIS, "600000"));
final boolean testWhileIdle = Boolean.parseBoolean(extensions.get(TEST_WHILE_IDLE, "true"));
this.component.getJmsProducerConnections().put(
provides,
new GenericObjectPool(new JMSProviderConnectionFactory(extensions,
this.component.getLogger()), maxActive, GenericObjectPool.WHEN_EXHAUSTED_BLOCK, maxWait,
maxIdle, false, false, timeBetweenEvictionTimeMillis, 5, minEvictableIdleTimeMillis,
testWhileIdle));
}
/**
* unRegister a pool of connections.
*
* @param provides
* @throws PEtALSCDKException
*/
private void unregisterConnectionPool(Provides provides) throws PEtALSCDKException {
// close pool
final GenericObjectPool pool = this.component.getJmsProducerConnections().get(provides);
if (pool != null) {
try {
pool.close();
} catch (final Exception e) {
throw new PEtALSCDKException("Can't close pool associated to provides node: " + provides);
}
}
// remove pool
this.component.getJmsProducerConnections().remove(provides);
}
}