Search in sources :

Example 1 with TimerSwiftlet

use of com.swiftmq.swiftlet.timer.TimerSwiftlet in project swiftmq-ce by iitsoftware.

the class ThreadpoolSwiftletImpl method startup.

protected void startup(Configuration config) throws SwiftletException {
    this.config = config;
    root = config;
    usageList = (EntityList) root.getEntity("usage");
    traceSwiftlet = (TraceSwiftlet) SwiftletManager.getInstance().getSwiftlet("sys$trace");
    traceSpace = traceSwiftlet.getTraceSpace(TraceSwiftlet.SPACE_KERNEL);
    if (traceSpace.enabled)
        traceSpace.trace(getName(), "startup ...");
    EntityList poolList = (EntityList) root.getEntity("pools");
    createPool(DEFAULT_POOL, null, poolList.getTemplate());
    PoolDispatcher dp = (PoolDispatcher) pools.get(DEFAULT_POOL);
    dp.setKernelPool(true);
    String[] poolNames = getDefinedPoolnames(poolList);
    if (poolNames != null && poolNames.length > 0) {
        if (traceSpace.enabled)
            traceSpace.trace(getName(), "startup: starting defined thread pools");
        for (int i = 0; i < poolNames.length; i++) {
            createPool(poolNames[i], poolList.getEntity(poolNames[i]), poolList.getTemplate());
            storeThreadNamesForPool(poolNames[i], poolList.getEntity(poolNames[i]));
        }
    }
    poolList.setEntityAddListener(new EntityChangeAdapter(null) {

        public void onEntityAdd(Entity parent, Entity newEntity) throws EntityAddException {
            boolean kp = ((Boolean) newEntity.getProperty(PROP_KERNEL_POOL).getValue()).booleanValue();
            if (kp)
                throw new EntityAddException("You cannot create a kernel pool dynamically.");
            createPool(newEntity.getName(), newEntity, newEntity);
            if (traceSpace.enabled)
                traceSpace.trace(getName(), "onEntityAdd (pool), poolName=" + newEntity.getName());
        }
    });
    poolList.setEntityRemoveListener(new EntityChangeAdapter(null) {

        public void onEntityRemove(Entity parent, Entity delEntity) throws EntityRemoveException {
            PoolDispatcher pd = null;
            synchronized (pools) {
                pd = (PoolDispatcher) pools.get(delEntity.getName());
                if (pd.isKernelPool())
                    throw new EntityRemoveException("You cannot remove a kernel pool dynamically.");
                pd.close();
                pools.remove(delEntity.getName());
                usageList.removeDynamicEntity(pd);
            }
            synchronized (threadNameMaps) {
                for (Iterator iter = threadNameMaps.entrySet().iterator(); iter.hasNext(); ) {
                    String entry = (String) ((Map.Entry) iter.next()).getValue();
                    if (entry.equals(pd.getPoolName()))
                        iter.remove();
                }
            }
            if (traceSpace.enabled)
                traceSpace.trace(getName(), "onEntityRemove (pool): poolName=" + delEntity.getName());
        }
    });
    try {
        SwiftletManager.getInstance().addSwiftletManagerListener("sys$mgmt", new SwiftletManagerAdapter() {

            public void swiftletStarted(SwiftletManagerEvent evt) {
                try {
                    timerSwiftlet = (TimerSwiftlet) SwiftletManager.getInstance().getSwiftlet("sys$timer");
                    mgmtSwiftlet = (MgmtSwiftlet) SwiftletManager.getInstance().getSwiftlet("sys$mgmt");
                    if (traceSpace.enabled)
                        traceSpace.trace(getName(), "registering MgmtListener ...");
                    mgmtSwiftlet.addMgmtListener(new MgmtListener() {

                        public void adminToolActivated() {
                            collectOn = true;
                            collectChanged(-1, collectInterval);
                        }

                        public void adminToolDeactivated() {
                            collectChanged(collectInterval, -1);
                            collectOn = false;
                        }
                    });
                } catch (Exception e) {
                    if (traceSpace.enabled)
                        traceSpace.trace(getName(), "swiftletStartet, exception=" + e);
                }
            }
        });
    } catch (Exception e) {
        throw new SwiftletException(e.getMessage());
    }
    Property prop = root.getProperty(PROP_COLLECT_INTERVAL);
    prop.setPropertyChangeListener(new PropertyChangeAdapter(null) {

        public void propertyChanged(Property property, Object oldValue, Object newValue) throws PropertyChangeException {
            collectInterval = ((Long) newValue).longValue();
            collectChanged(((Long) oldValue).longValue(), collectInterval);
        }
    });
    collectInterval = ((Long) prop.getValue()).longValue();
    if (collectOn) {
        if (collectInterval > 0) {
            if (traceSpace.enabled)
                traceSpace.trace(getName(), "startup: registering thread count collector");
            timerSwiftlet.addTimerListener(collectInterval, this);
        } else if (traceSpace.enabled)
            traceSpace.trace(getName(), "startup: collect interval <= 0; no thread count collector");
    }
}
Also used : MgmtListener(com.swiftmq.swiftlet.mgmt.event.MgmtListener) MgmtSwiftlet(com.swiftmq.swiftlet.mgmt.MgmtSwiftlet) SwiftletException(com.swiftmq.swiftlet.SwiftletException) SwiftletManagerAdapter(com.swiftmq.swiftlet.event.SwiftletManagerAdapter) SwiftletManagerEvent(com.swiftmq.swiftlet.event.SwiftletManagerEvent) SwiftletException(com.swiftmq.swiftlet.SwiftletException) Iterator(java.util.Iterator) TimerSwiftlet(com.swiftmq.swiftlet.timer.TimerSwiftlet)

Aggregations

SwiftletException (com.swiftmq.swiftlet.SwiftletException)1 SwiftletManagerAdapter (com.swiftmq.swiftlet.event.SwiftletManagerAdapter)1 SwiftletManagerEvent (com.swiftmq.swiftlet.event.SwiftletManagerEvent)1 MgmtSwiftlet (com.swiftmq.swiftlet.mgmt.MgmtSwiftlet)1 MgmtListener (com.swiftmq.swiftlet.mgmt.event.MgmtListener)1 TimerSwiftlet (com.swiftmq.swiftlet.timer.TimerSwiftlet)1 Iterator (java.util.Iterator)1