Search in sources :

Example 1 with TimerServiceWrapper

use of org.apache.openejb.core.timer.TimerServiceWrapper in project tomee by apache.

the class SingletonInstanceManager method deploy.

public void deploy(final BeanContext beanContext) throws OpenEJBException {
    final Data data = new Data(beanContext);
    beanContext.setContainerData(data);
    beanContext.set(EJBContext.class, this.sessionContext);
    // Create stats interceptor
    if (StatsInterceptor.isStatsActivated()) {
        final StatsInterceptor stats = new StatsInterceptor(beanContext.getBeanClass());
        beanContext.addFirstSystemInterceptor(stats);
        final ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management");
        jmxName.set("J2EEServer", "openejb");
        jmxName.set("J2EEApplication", null);
        jmxName.set("EJBModule", beanContext.getModuleID());
        jmxName.set("SingletonSessionBean", beanContext.getEjbName());
        jmxName.set("name", beanContext.getEjbName());
        jmxName.set("j2eeType", "Invocations");
        // register the invocation stats interceptor
        final MBeanServer server = LocalMBeanServer.get();
        try {
            final ObjectName objectName = jmxName.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);
        }
    }
    try {
        final Context context = beanContext.getJndiEnc();
        context.bind("comp/EJBContext", sessionContext);
        context.bind("comp/WebServiceContext", webServiceContext);
        context.bind("comp/TimerService", new TimerServiceWrapper());
    } catch (final NamingException e) {
        throw new OpenEJBException("Failed to bind EJBContext/WebServiceContext/TimerService", e);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) WebServiceContext(javax.xml.ws.WebServiceContext) Context(javax.naming.Context) EJBContext(javax.ejb.EJBContext) InstanceContext(org.apache.openejb.core.InstanceContext) ThreadContext(org.apache.openejb.core.ThreadContext) ObjectNameBuilder(org.apache.openejb.monitoring.ObjectNameBuilder) TimerServiceWrapper(org.apache.openejb.core.timer.TimerServiceWrapper) OpenEJBException(org.apache.openejb.OpenEJBException) StatsInterceptor(org.apache.openejb.monitoring.StatsInterceptor) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) NamingException(javax.naming.NamingException) ManagedMBean(org.apache.openejb.monitoring.ManagedMBean) NoSuchEJBException(javax.ejb.NoSuchEJBException) NamingException(javax.naming.NamingException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBException(org.apache.openejb.OpenEJBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExecutionException(java.util.concurrent.ExecutionException) MBeanServer(javax.management.MBeanServer) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) ObjectName(javax.management.ObjectName)

Example 2 with TimerServiceWrapper

use of org.apache.openejb.core.timer.TimerServiceWrapper 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

InvocationTargetException (java.lang.reflect.InvocationTargetException)2 EJBContext (javax.ejb.EJBContext)2 MBeanServer (javax.management.MBeanServer)2 ObjectName (javax.management.ObjectName)2 Context (javax.naming.Context)2 NamingException (javax.naming.NamingException)2 ApplicationException (org.apache.openejb.ApplicationException)2 BeanContext (org.apache.openejb.BeanContext)2 OpenEJBException (org.apache.openejb.OpenEJBException)2 InstanceContext (org.apache.openejb.core.InstanceContext)2 ThreadContext (org.apache.openejb.core.ThreadContext)2 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)2 TimerServiceWrapper (org.apache.openejb.core.timer.TimerServiceWrapper)2 LocalMBeanServer (org.apache.openejb.monitoring.LocalMBeanServer)2 ManagedMBean (org.apache.openejb.monitoring.ManagedMBean)2 ObjectNameBuilder (org.apache.openejb.monitoring.ObjectNameBuilder)2 StatsInterceptor (org.apache.openejb.monitoring.StatsInterceptor)2 IOException (java.io.IOException)1 RemoteException (java.rmi.RemoteException)1 ExecutionException (java.util.concurrent.ExecutionException)1