Search in sources :

Example 61 with OpenEJBException

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;
        }
    });
}
Also used : SystemInstance(org.apache.openejb.loader.SystemInstance) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) NamingException(javax.naming.NamingException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) EJBAccessException(javax.ejb.EJBAccessException) RemoveException(javax.ejb.RemoveException) OpenEJBException(org.apache.openejb.OpenEJBException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) SystemException(org.apache.openejb.SystemException) NoSuchObjectException(java.rmi.NoSuchObjectException) EntityManagerAlreadyRegisteredException(org.apache.openejb.persistence.EntityManagerAlreadyRegisteredException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) ObjectName(javax.management.ObjectName)

Example 62 with OpenEJBException

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();
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) Options(org.apache.openejb.loader.Options) StatsInterceptor(org.apache.openejb.monitoring.StatsInterceptor) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) ReflectionException(javax.management.ReflectionException) OpenEJBException(org.apache.openejb.OpenEJBException) UnavailableException(javax.resource.spi.UnavailableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedObjectNameException(javax.management.MalformedObjectNameException) SystemException(org.apache.openejb.SystemException) AttributeNotFoundException(javax.management.AttributeNotFoundException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ApplicationException(org.apache.openejb.ApplicationException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ConstraintViolationException(javax.validation.ConstraintViolationException) ObjectName(javax.management.ObjectName) ObjectNameBuilder(org.apache.openejb.monitoring.ObjectNameBuilder) ActivationSpec(javax.resource.spi.ActivationSpec) ResourceException(javax.resource.ResourceException) ManagedMBean(org.apache.openejb.monitoring.ManagedMBean) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer)

Example 63 with OpenEJBException

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());
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) ResourceException(javax.resource.ResourceException) NamingException(javax.naming.NamingException) ReflectionException(javax.management.ReflectionException) OpenEJBException(org.apache.openejb.OpenEJBException) UnavailableException(javax.resource.spi.UnavailableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MalformedObjectNameException(javax.management.MalformedObjectNameException) SystemException(org.apache.openejb.SystemException) AttributeNotFoundException(javax.management.AttributeNotFoundException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) ApplicationException(org.apache.openejb.ApplicationException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ConstraintViolationException(javax.validation.ConstraintViolationException) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer)

Example 64 with OpenEJBException

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();
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) Options(org.apache.openejb.loader.Options) ObjectNameBuilder(org.apache.openejb.monitoring.ObjectNameBuilder) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) Pool(org.apache.openejb.util.Pool) ResourceException(javax.resource.ResourceException) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) StatsInterceptor(org.apache.openejb.monitoring.StatsInterceptor) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) IOException(java.io.IOException) Flushable(java.io.Flushable) ResourceException(javax.resource.ResourceException) TimeoutException(java.util.concurrent.TimeoutException) ReflectionException(javax.management.ReflectionException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) MalformedObjectNameException(javax.management.MalformedObjectNameException) SystemException(org.apache.openejb.SystemException) AttributeNotFoundException(javax.management.AttributeNotFoundException) ApplicationException(org.apache.openejb.ApplicationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) IOException(java.io.IOException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) ObjectName(javax.management.ObjectName) ObjectNameBuilder(org.apache.openejb.monitoring.ObjectNameBuilder) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ManagedMBean(org.apache.openejb.monitoring.ManagedMBean)

Example 65 with OpenEJBException

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);
            }
        }
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) OpenEJBException(org.apache.openejb.OpenEJBException) EjbTransactionUtil.handleApplicationException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException) ApplicationException(org.apache.openejb.ApplicationException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) SystemException(org.apache.openejb.SystemException) ThreadContext(org.apache.openejb.core.ThreadContext) Operation(org.apache.openejb.core.Operation) Method(java.lang.reflect.Method)

Aggregations

OpenEJBException (org.apache.openejb.OpenEJBException)187 IOException (java.io.IOException)55 NamingException (javax.naming.NamingException)34 URL (java.net.URL)31 MalformedURLException (java.net.MalformedURLException)30 ApplicationException (org.apache.openejb.ApplicationException)29 BeanContext (org.apache.openejb.BeanContext)29 File (java.io.File)26 ArrayList (java.util.ArrayList)26 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)23 SystemException (org.apache.openejb.SystemException)22 Method (java.lang.reflect.Method)20 HashMap (java.util.HashMap)18 ThreadContext (org.apache.openejb.core.ThreadContext)17 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)15 RemoteException (java.rmi.RemoteException)14 EJBException (javax.ejb.EJBException)14 EjbTransactionUtil.handleApplicationException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleApplicationException)14 HashSet (java.util.HashSet)13 Properties (java.util.Properties)13