use of org.apache.openejb.OpenEJBException in project tomee by apache.
the class ManagedContainer method undeploy.
@Override
public synchronized void undeploy(final BeanContext bean) throws OpenEJBException {
final Data data = (Data) bean.getContainerData();
if (data != null) {
final MBeanServer server = LocalMBeanServer.get();
for (final ObjectName objectName : data.jmxNames) {
try {
server.unregisterMBean(objectName);
} catch (final Exception e) {
logger.error("Unable to unregister MBean " + objectName);
}
}
}
deploymentsById.remove(bean.getDeploymentID());
bean.setContainer(null);
bean.setContainerData(null);
cache.removeAll(new CacheFilter<Instance>() {
@Override
public boolean matches(final Instance instance) {
return bean == instance.beanContext;
}
});
}
use of org.apache.openejb.OpenEJBException in project tomee by apache.
the class MdbContainer method deploy.
public void deploy(final BeanContext beanContext) throws OpenEJBException {
final Object deploymentId = beanContext.getDeploymentID();
if (!beanContext.getMdbInterface().equals(messageListenerInterface)) {
throw new OpenEJBException("Deployment '" + deploymentId + "' has message listener interface " + beanContext.getMdbInterface().getName() + " but this MDB container only supports " + messageListenerInterface);
}
// create the activation spec
final ActivationSpec activationSpec = createActivationSpec(beanContext);
if (inboundRecovery != null) {
inboundRecovery.recover(resourceAdapter, activationSpec, containerID.toString());
}
final Options options = new Options(beanContext.getProperties());
final int instanceLimit = options.get("InstanceLimit", this.instanceLimit);
// create the message endpoint
final MdbInstanceFactory instanceFactory = new MdbInstanceFactory(beanContext, securityService, instanceLimit);
final EndpointFactory endpointFactory = new EndpointFactory(activationSpec, this, beanContext, instanceFactory, null, xaResourceWrapper, false);
// update the data structures
// this must be done before activating the endpoint since the ra may immedately begin delivering messages
beanContext.setContainer(this);
beanContext.setContainerData(endpointFactory);
deployments.put(deploymentId, beanContext);
// Create stats interceptor
if (StatsInterceptor.isStatsActivated()) {
final StatsInterceptor stats = new StatsInterceptor(beanContext.getBeanClass());
beanContext.addFirstSystemInterceptor(stats);
final MBeanServer server = LocalMBeanServer.get();
final ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management");
jmxName.set("J2EEServer", "openejb");
jmxName.set("J2EEApplication", null);
jmxName.set("EJBModule", beanContext.getModuleID());
jmxName.set("StatelessSessionBean", beanContext.getEjbName());
jmxName.set("j2eeType", "");
jmxName.set("name", beanContext.getEjbName());
// register the invocation stats interceptor
try {
final ObjectName objectName = jmxName.set("j2eeType", "Invocations").build();
if (server.isRegistered(objectName)) {
server.unregisterMBean(objectName);
}
server.registerMBean(new ManagedMBean(stats), objectName);
endpointFactory.jmxNames.add(objectName);
} catch (final Exception e) {
logger.error("Unable to register MBean ", e);
}
}
// activate the endpoint
CURRENT.set(beanContext);
try {
final MdbActivationContext activationContext = new MdbActivationContext(Thread.currentThread().getContextClassLoader(), beanContext, resourceAdapter, endpointFactory, activationSpec);
activationContexts.put(beanContext, activationContext);
boolean activeOnStartup = true;
String activeOnStartupSetting = beanContext.getActivationProperties().get("MdbActiveOnStartup");
if (activeOnStartupSetting == null) {
activeOnStartupSetting = beanContext.getActivationProperties().get("DeliveryActive");
}
if (activeOnStartupSetting != null) {
activeOnStartup = Boolean.parseBoolean(activeOnStartupSetting);
}
if (activeOnStartup) {
activationContext.start();
} else {
logger.info("Not auto-activating endpoint for " + beanContext.getDeploymentID());
}
String jmxName = beanContext.getActivationProperties().get("MdbJMXControl");
if (jmxName == null) {
jmxName = "true";
}
addJMxControl(beanContext, jmxName, activationContext);
} catch (final ResourceException e) {
// activation failed... clean up
beanContext.setContainer(null);
beanContext.setContainerData(null);
deployments.remove(deploymentId);
throw new OpenEJBException(e);
} finally {
CURRENT.remove();
}
}
use of org.apache.openejb.OpenEJBException in project tomee by apache.
the class MdbContainer method undeploy.
public void undeploy(final BeanContext beanContext) throws OpenEJBException {
if (!(beanContext instanceof BeanContext)) {
return;
}
try {
final EndpointFactory endpointFactory = (EndpointFactory) beanContext.getContainerData();
if (endpointFactory != null) {
CURRENT.set(beanContext);
try {
final ObjectName jmxBeanToRemove = mbeanNames.remove(beanContext);
if (jmxBeanToRemove != null) {
LocalMBeanServer.unregisterSilently(jmxBeanToRemove);
logger.info("Undeployed MDB control for " + beanContext.getDeploymentID());
}
final MdbActivationContext activationContext = activationContexts.remove(beanContext);
if (activationContext != null && activationContext.isStarted()) {
resourceAdapter.endpointDeactivation(endpointFactory, endpointFactory.getActivationSpec());
}
} finally {
CURRENT.remove();
}
final MBeanServer server = LocalMBeanServer.get();
for (final ObjectName objectName : endpointFactory.jmxNames) {
try {
server.unregisterMBean(objectName);
} catch (final Exception e) {
logger.error("Unable to unregister MBean " + objectName);
}
}
}
} finally {
beanContext.setContainer(null);
beanContext.setContainerData(null);
deployments.remove(beanContext.getDeploymentID());
}
}
use of org.apache.openejb.OpenEJBException in project tomee by apache.
the class MdbInstanceManager method deploy.
public void deploy(final BeanContext beanContext, final ActivationSpec activationSpec, final EndpointFactory endpointFactory) throws OpenEJBException {
if (inboundRecovery != null) {
inboundRecovery.recover(resourceAdapter, activationSpec, containerID.toString());
}
final ObjectRecipe recipe = PassthroughFactory.recipe(new Pool.Builder(poolBuilder));
recipe.allow(Option.CASE_INSENSITIVE_FACTORY);
recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
recipe.setAllProperties(beanContext.getProperties());
final Pool.Builder builder = (Pool.Builder) recipe.create();
setDefault(builder.getMaxAge(), TimeUnit.HOURS);
setDefault(builder.getIdleTimeout(), TimeUnit.MINUTES);
setDefault(builder.getInterval(), TimeUnit.MINUTES);
final InstanceSupplier supplier = new InstanceSupplier(beanContext);
builder.setSupplier(supplier);
builder.setExecutor(executor);
builder.setScheduledExecutor(scheduledExecutor);
final int min = builder.getMin();
final long maxAge = builder.getMaxAge().getTime(TimeUnit.MILLISECONDS);
final double maxAgeOffset = builder.getMaxAgeOffset();
final Data data = new Data(builder.build(), accessTimeout, closeTimeout);
MdbContext mdbContext = new MdbContext(securityService, new Flushable() {
@Override
public void flush() throws IOException {
data.flush();
}
});
data.setBaseContext(mdbContext);
beanContext.setContainerData(data);
final MBeanServer server = LocalMBeanServer.get();
final ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management");
jmxName.set("J2EEServer", "openejb");
jmxName.set("J2EEApplication", null);
jmxName.set("EJBModule", beanContext.getModuleID());
jmxName.set("MessageDrivenBean", beanContext.getEjbName());
jmxName.set("j2eeType", "");
jmxName.set("name", beanContext.getEjbName());
// Create stats interceptor
if (StatsInterceptor.isStatsActivated()) {
final StatsInterceptor stats = new StatsInterceptor(beanContext.getBeanClass());
beanContext.addFirstSystemInterceptor(stats);
// register the invocation stats interceptor
try {
final ObjectName objectName = jmxName.set("j2eeType", "Invocations").build();
if (server.isRegistered(objectName)) {
server.unregisterMBean(objectName);
}
server.registerMBean(new ManagedMBean(stats), objectName);
jmxNames.add(objectName);
} catch (final Exception e) {
logger.error("Unable to register MBean ", e);
}
}
// activate the endpoint
try {
final MdbPoolContainer.MdbActivationContext activationContext = new MdbPoolContainer.MdbActivationContext(Thread.currentThread().getContextClassLoader(), beanContext, resourceAdapter, endpointFactory, activationSpec);
activationContexts.put(beanContext, activationContext);
boolean activeOnStartup = true;
String activeOnStartupSetting = beanContext.getActivationProperties().get("MdbActiveOnStartup");
if (activeOnStartupSetting == null) {
activeOnStartupSetting = beanContext.getActivationProperties().get("DeliveryActive");
}
if (activeOnStartupSetting != null) {
activeOnStartup = Boolean.parseBoolean(activeOnStartupSetting);
}
if (activeOnStartup) {
activationContext.start();
} else {
logger.info("Not auto-activating endpoint for " + beanContext.getDeploymentID());
}
String jmxControlName = beanContext.getActivationProperties().get("MdbJMXControl");
if (jmxControlName == null) {
jmxControlName = "true";
}
addJMxControl(beanContext, jmxControlName, activationContext);
} catch (final ResourceException e) {
throw new OpenEJBException(e);
}
final Options options = new Options(beanContext.getProperties());
// Finally, fill the pool and start it
if (!options.get("BackgroundStartup", false) && min > 0) {
final ExecutorService es = Executors.newFixedThreadPool(min);
for (int i = 0; i < min; i++) {
es.submit(new InstanceCreatorRunnable(maxAge, i, min, maxAgeOffset, data, supplier));
}
es.shutdown();
try {
es.awaitTermination(5, TimeUnit.MINUTES);
} catch (final InterruptedException e) {
logger.error("can't fill the message driven bean pool", e);
}
}
// register the pool
try {
final ObjectName objectName = jmxName.set("j2eeType", "Pool").build();
if (server.isRegistered(objectName)) {
server.unregisterMBean(objectName);
}
server.registerMBean(new ManagedMBean(data.pool), objectName);
data.add(objectName);
} catch (final Exception e) {
logger.error("Unable to register MBean ", e);
}
data.getPool().start();
}
use of org.apache.openejb.OpenEJBException in project tomee by apache.
the class MdbPoolContainer method invoke.
public Object invoke(final Object instance, final Method method, final InterfaceType type, Object... args) throws SystemException, ApplicationException {
if (args == null) {
args = NO_ARGS;
}
// get the context data
final ThreadContext callContext = ThreadContext.getThreadContext();
final BeanContext deployInfo = callContext.getBeanContext();
final MdbCallContext mdbCallContext = callContext.get(MdbCallContext.class);
if (mdbCallContext == null) {
throw new IllegalStateException("beforeDelivery was not called");
}
// verify the delivery method passed to beforeDeliver is the same method that was invoked
if (!mdbCallContext.deliveryMethod.getName().equals(method.getName()) || !Arrays.deepEquals(mdbCallContext.deliveryMethod.getParameterTypes(), method.getParameterTypes())) {
throw new IllegalStateException("Delivery method specified in beforeDelivery is not the delivery method called");
}
// remember the return value or exception so it can be logged
Object returnValue = null;
OpenEJBException openEjbException = null;
final Operation oldOperation = callContext.getCurrentOperation();
callContext.setCurrentOperation(type == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS);
try {
if (logger.isDebugEnabled()) {
logger.info("invoking method " + method.getName() + " on " + deployInfo.getDeploymentID());
}
// determine the target method on the bean instance class
final Method targetMethod = deployInfo.getMatchingBeanMethod(method);
callContext.set(Method.class, targetMethod);
// invoke the target method
returnValue = _invoke(instance, targetMethod, args, deployInfo, type, mdbCallContext, callContext);
return returnValue;
} catch (final ApplicationException | SystemException e) {
openEjbException = e;
throw e;
} finally {
callContext.setCurrentOperation(oldOperation);
// Log the invocation results
if (logger.isDebugEnabled()) {
if (openEjbException == null) {
logger.debug("finished invoking method " + method.getName() + ". Return value:" + returnValue);
} else {
final Throwable exception = openEjbException.getRootCause() != null ? openEjbException.getRootCause() : openEjbException;
logger.debug("finished invoking method " + method.getName() + " with exception " + exception);
}
}
}
}
Aggregations