Search in sources :

Example 1 with Pool

use of org.apache.openejb.util.Pool 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 2 with Pool

use of org.apache.openejb.util.Pool in project tomee by apache.

the class MdbInstanceManager method getInstance.

/**
 * Removes an instance from the pool and returns it for use
 * by the container in business methods.
 * <p/>
 * If the pool is at it's limit the StrictPooling flag will
 * cause this thread to wait.
 * <p/>
 * If StrictPooling is not enabled this method will create a
 * new bean instance performing all required injection
 * and callbacks before returning it in a method ready state.
 *
 * @param callContext ThreadContext
 * @return Object
 * @throws OpenEJBException
 */
public Instance getInstance(final ThreadContext callContext) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final Data data = (Data) beanContext.getContainerData();
    Instance instance = null;
    try {
        final Pool<Instance>.Entry entry = data.poolPop();
        if (entry != null) {
            instance = entry.get();
            instance.setPoolEntry(entry);
        }
    } catch (final TimeoutException e) {
        final String msg = "No instances available in Message Driven Bean pool.  Waited " + data.getAccessTimeout().toString();
        final ConcurrentAccessTimeoutException timeoutException = new ConcurrentAccessTimeoutException(msg);
        timeoutException.fillInStackTrace();
        throw new ApplicationException(timeoutException);
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new OpenEJBException("Unexpected Interruption of current thread: ", e);
    }
    if (null == instance) {
        instance = createInstance(beanContext);
    }
    return instance;
}
Also used : BeanContext(org.apache.openejb.BeanContext) OpenEJBException(org.apache.openejb.OpenEJBException) ApplicationException(org.apache.openejb.ApplicationException) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) Pool(org.apache.openejb.util.Pool) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) TimeoutException(java.util.concurrent.TimeoutException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException)

Example 3 with Pool

use of org.apache.openejb.util.Pool in project tomee by apache.

the class StatelessInstanceManager method getInstance.

/**
 * Removes an instance from the pool and returns it for use
 * by the container in business methods.
 * <p/>
 * If the pool is at it's limit the StrictPooling flag will
 * cause this thread to wait.
 * <p/>
 * If StrictPooling is not enabled this method will create a
 * new stateless bean instance performing all required injection
 * and callbacks before returning it in a method ready state.
 *
 * @param callContext ThreadContext
 * @return Object
 * @throws OpenEJBException
 */
public Instance getInstance(final ThreadContext callContext) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final Data data = (Data) beanContext.getContainerData();
    Instance instance = null;
    try {
        final Pool<Instance>.Entry entry = data.poolPop();
        if (entry != null) {
            instance = entry.get();
            instance.setPoolEntry(entry);
        }
    } catch (final TimeoutException e) {
        final String msg = "No instances available in Stateless Session Bean pool.  Waited " + data.accessTimeout.toString();
        final ConcurrentAccessTimeoutException timeoutException = new ConcurrentAccessTimeoutException(msg);
        timeoutException.fillInStackTrace();
        throw new ApplicationException(timeoutException);
    } catch (final InterruptedException e) {
        Thread.interrupted();
        throw new OpenEJBException("Unexpected Interruption of current thread: ", e);
    }
    if (null == instance) {
        instance = createInstance(callContext, beanContext);
    }
    return instance;
}
Also used : BeanContext(org.apache.openejb.BeanContext) OpenEJBException(org.apache.openejb.OpenEJBException) ApplicationException(org.apache.openejb.ApplicationException) InterceptorInstance(org.apache.openejb.core.interceptor.InterceptorInstance) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) Pool(org.apache.openejb.util.Pool) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) TimeoutException(java.util.concurrent.TimeoutException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException)

Example 4 with Pool

use of org.apache.openejb.util.Pool in project tomee by apache.

the class StatelessInstanceManager method deploy.

@SuppressWarnings("unchecked")
public void deploy(final BeanContext beanContext) throws OpenEJBException {
    final Options options = new Options(beanContext.getProperties());
    final Duration accessTimeout = getDuration(options, "AccessTimeout", // default timeout
    getDuration(options, "Timeout", this.accessTimeout, TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
    final Duration closeTimeout = getDuration(options, "CloseTimeout", this.closeTimeout, TimeUnit.MINUTES);
    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 StatelessSupplier supplier = new StatelessSupplier(beanContext);
    builder.setSupplier(supplier);
    builder.setExecutor(executor);
    builder.setScheduledExecutor(scheduledExecutor);
    final Data data = new Data(builder.build(), accessTimeout, closeTimeout);
    beanContext.setContainerData(data);
    beanContext.set(EJBContext.class, data.sessionContext);
    try {
        final Context context = beanContext.getJndiEnc();
        context.bind("comp/EJBContext", data.sessionContext);
        context.bind("comp/WebServiceContext", new EjbWsContext(data.sessionContext));
        context.bind("comp/TimerService", new TimerServiceWrapper());
    } catch (final NamingException e) {
        throw new OpenEJBException("Failed to bind EJBContext/WebServiceContext/TimerService", e);
    }
    final int min = builder.getMin();
    final long maxAge = builder.getMaxAge().getTime(TimeUnit.MILLISECONDS);
    final double maxAgeOffset = builder.getMaxAgeOffset();
    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("name", beanContext.getEjbName());
    final MBeanServer server = LocalMBeanServer.get();
    // Create stats interceptor
    if (StatsInterceptor.isStatsActivated()) {
        StatsInterceptor stats = null;
        for (final InterceptorInstance interceptor : beanContext.getUserAndSystemInterceptors()) {
            if (interceptor.getInterceptor() instanceof StatsInterceptor) {
                stats = (StatsInterceptor) interceptor.getInterceptor();
            }
        }
        if (stats == null) {
            // normally useless
            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);
            data.add(objectName);
        } catch (final Exception e) {
            logger.error("Unable to register MBean ", 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);
    }
    // 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 stateless pool", e);
        }
    }
    data.getPool().start();
}
Also used : Options(org.apache.openejb.loader.Options) OpenEJBException(org.apache.openejb.OpenEJBException) ObjectNameBuilder(org.apache.openejb.monitoring.ObjectNameBuilder) TimerServiceWrapper(org.apache.openejb.core.timer.TimerServiceWrapper) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) Pool(org.apache.openejb.util.Pool) NamingException(javax.naming.NamingException) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) SessionContext(javax.ejb.SessionContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) EJBContext(javax.ejb.EJBContext) InstanceContext(org.apache.openejb.core.InstanceContext) ThreadContext(org.apache.openejb.core.ThreadContext) StatsInterceptor(org.apache.openejb.monitoring.StatsInterceptor) Duration(org.apache.openejb.util.Duration) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorInstance(org.apache.openejb.core.interceptor.InterceptorInstance) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) SystemException(org.apache.openejb.SystemException) ApplicationException(org.apache.openejb.ApplicationException) IOException(java.io.IOException) 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)

Aggregations

TimeoutException (java.util.concurrent.TimeoutException)4 ConcurrentAccessTimeoutException (javax.ejb.ConcurrentAccessTimeoutException)4 ApplicationException (org.apache.openejb.ApplicationException)4 OpenEJBException (org.apache.openejb.OpenEJBException)4 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)4 Pool (org.apache.openejb.util.Pool)4 BeanContext (org.apache.openejb.BeanContext)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 RemoteException (java.rmi.RemoteException)2 ExecutorService (java.util.concurrent.ExecutorService)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 MBeanServer (javax.management.MBeanServer)2 ObjectName (javax.management.ObjectName)2 SystemException (org.apache.openejb.SystemException)2 InterceptorInstance (org.apache.openejb.core.interceptor.InterceptorInstance)2 Options (org.apache.openejb.loader.Options)2 LocalMBeanServer (org.apache.openejb.monitoring.LocalMBeanServer)2 ManagedMBean (org.apache.openejb.monitoring.ManagedMBean)2 ObjectNameBuilder (org.apache.openejb.monitoring.ObjectNameBuilder)2