Search in sources :

Example 1 with Connection

use of com.swiftmq.swiftlet.net.Connection in project swiftmq-ce by iitsoftware.

the class MQTTSwiftlet method startup.

@Override
protected void startup(Configuration config) throws SwiftletException {
    try {
        ctx = new SwiftletContext(config, this);
    } catch (Exception e) {
        e.printStackTrace();
        throw new SwiftletException(e.getMessage());
    }
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(getName(), "startup ...");
    createListenerAdapter((EntityList) ctx.root.getEntity("listeners"));
    Property prop = ctx.root.getProperty("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 (ctx.traceSpace.enabled)
                ctx.traceSpace.trace(getName(), "startup: registering msg/s count collector");
            ctx.timerSwiftlet.addTimerListener(collectInterval, this);
        } else if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace(getName(), "startup: collect interval <= 0; no msg/s count collector");
    }
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(getName(), "registering MgmtListener ...");
    ctx.mgmtSwiftlet.addMgmtListener(this);
    ctx.usageListConnections.setEntityRemoveListener(new EntityChangeAdapter(null) {

        public void onEntityRemove(Entity parent, Entity delEntity) throws EntityRemoveException {
            Connection myConnection = (Connection) delEntity.getDynamicObject();
            ConnectionManager connectionManager = ctx.networkSwiftlet.getConnectionManager();
            connectionManager.removeConnection(myConnection);
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace(getName(), "onEntityRemove (Connection): " + myConnection);
        }
    });
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(getName(), "startup done.");
}
Also used : MQTTConnection(com.swiftmq.impl.mqtt.connection.MQTTConnection) Connection(com.swiftmq.swiftlet.net.Connection) SwiftletException(com.swiftmq.swiftlet.SwiftletException) ConnectionVetoException(com.swiftmq.swiftlet.net.ConnectionVetoException) ConnectionManager(com.swiftmq.swiftlet.net.ConnectionManager) SwiftletException(com.swiftmq.swiftlet.SwiftletException)

Example 2 with Connection

use of com.swiftmq.swiftlet.net.Connection in project swiftmq-ce by iitsoftware.

the class MQTTSwiftlet method shutdown.

@Override
protected void shutdown() throws SwiftletException {
    // true when shutdown while standby
    if (ctx == null)
        return;
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(getName(), "shutdown ...");
    try {
        collectChanged(collectInterval, -1);
        listenerAdapter.close();
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace(getName(), "shutdown: shutdown all MQTT connections");
        Semaphore sem = getShutdownSemaphore();
        ConnectionManager connectionManager = ctx.networkSwiftlet.getConnectionManager();
        Connection[] c = (Connection[]) connections.toArray(new Connection[connections.size()]);
        connections.clear();
        for (int i = 0; i < c.length; i++) {
            connectionManager.removeConnection(c[i]);
        }
        if (sem != null) {
            System.out.println("+++ waiting for connection shutdown ...");
            sem.waitHere();
            try {
                Thread.sleep(5000);
            } catch (Exception ignored) {
            }
        }
        ctx.mgmtSwiftlet.removeMgmtListener(this);
        ctx.sessionRegistry.close();
        ctx.retainer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(getName(), "shutdown done.");
    ctx = null;
}
Also used : ConnectionManager(com.swiftmq.swiftlet.net.ConnectionManager) MQTTConnection(com.swiftmq.impl.mqtt.connection.MQTTConnection) Connection(com.swiftmq.swiftlet.net.Connection) Semaphore(com.swiftmq.tools.concurrent.Semaphore) SwiftletException(com.swiftmq.swiftlet.SwiftletException) ConnectionVetoException(com.swiftmq.swiftlet.net.ConnectionVetoException)

Example 3 with Connection

use of com.swiftmq.swiftlet.net.Connection in project swiftmq-ce by iitsoftware.

the class ConnectionManagerImpl method collectByteCounts.

public synchronized void collectByteCounts() {
    long actTime = System.currentTimeMillis();
    double deltas = (actTime - lastCollectTime) / 1000.0;
    for (Object o : ctx.usageList.getEntities().entrySet()) {
        Entity entity = (Entity) ((Map.Entry) o).getValue();
        Property input = entity.getProperty("throughput-input");
        Property output = entity.getProperty("throughput-output");
        Connection connection = (Connection) entity.getDynamicObject();
        if (connection != null) {
            Countable in = (Countable) connection.getInputStream();
            Countable out = (Countable) connection.getOutputStream();
            if (lastCollectTime != -1) {
                try {
                    input.setValue(formatter.format(new Double(((double) in.getByteCount() / 1024.0) / deltas)));
                    output.setValue(formatter.format(new Double(((double) out.getByteCount() / 1024.0) / deltas)));
                } catch (Exception ignored) {
                }
            } else {
                try {
                    input.setValue(0.0);
                    output.setValue(0.0);
                } catch (Exception ignored) {
                }
            }
            in.resetByteCount();
            out.resetByteCount();
        }
    }
    lastCollectTime = actTime;
}
Also used : Entity(com.swiftmq.mgmt.Entity) Connection(com.swiftmq.swiftlet.net.Connection) Property(com.swiftmq.mgmt.Property) EntityRemoveException(com.swiftmq.mgmt.EntityRemoveException)

Example 4 with Connection

use of com.swiftmq.swiftlet.net.Connection in project swiftmq-ce by iitsoftware.

the class AMQPSwiftlet method startup.

protected void startup(Configuration configuration) throws SwiftletException {
    ctx = new SwiftletContext(configuration, this);
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(getName(), "startup ...");
    Security.addProvider(new SASLProvider(SASLProvider.PROVIDER_NAME, 1.0, "SwiftMQ SASL Security Provider"));
    createListenerAdapter((EntityList) ctx.root.getEntity("listeners"));
    Property prop = ctx.root.getProperty("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 (ctx.traceSpace.enabled)
                ctx.traceSpace.trace(getName(), "startup: registering msg/s count collector");
            ctx.timerSwiftlet.addTimerListener(collectInterval, this);
        } else if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace(getName(), "startup: collect interval <= 0; no msg/s count collector");
    }
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(getName(), "registering MgmtListener ...");
    ctx.mgmtSwiftlet.addMgmtListener(this);
    ctx.usageList.setEntityRemoveListener(new EntityChangeAdapter(null) {

        public void onEntityRemove(Entity parent, Entity delEntity) throws EntityRemoveException {
            Connection myConnection = (Connection) delEntity.getDynamicObject();
            ConnectionManager connectionManager = ctx.networkSwiftlet.getConnectionManager();
            connectionManager.removeConnection(myConnection);
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace(getName(), "onEntityRemove (Connection): " + myConnection);
        }
    });
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(getName(), "startup done.");
}
Also used : SASLProvider(com.swiftmq.impl.amqp.sasl.provider.SASLProvider) ConnectionManager(com.swiftmq.swiftlet.net.ConnectionManager) Connection(com.swiftmq.swiftlet.net.Connection)

Example 5 with Connection

use of com.swiftmq.swiftlet.net.Connection in project swiftmq-ce by iitsoftware.

the class AMQPSwiftlet method shutdown.

protected void shutdown() throws SwiftletException {
    // true when shutdown while standby
    if (ctx == null)
        return;
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(getName(), "shutdown ...");
    try {
        listenerAdapter.close();
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace(getName(), "shutdown: shutdown all AMQP connections");
        Semaphore sem = getShutdownSemaphore();
        ConnectionManager connectionManager = ctx.networkSwiftlet.getConnectionManager();
        Connection[] c = (Connection[]) connections.toArray(new Connection[connections.size()]);
        connections.clear();
        for (int i = 0; i < c.length; i++) {
            connectionManager.removeConnection(c[i]);
        }
        if (sem != null) {
            System.out.println("+++ waiting for connection shutdown ...");
            sem.waitHere();
            try {
                Thread.sleep(5000);
            } catch (Exception ignored) {
            }
        }
        ctx.mgmtSwiftlet.removeMgmtListener(this);
        Security.removeProvider(SASLProvider.PROVIDER_NAME);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(getName(), "shutdown done.");
    ctx = null;
}
Also used : ConnectionManager(com.swiftmq.swiftlet.net.ConnectionManager) Connection(com.swiftmq.swiftlet.net.Connection) Semaphore(com.swiftmq.tools.concurrent.Semaphore) SwiftletException(com.swiftmq.swiftlet.SwiftletException) ConnectionVetoException(com.swiftmq.swiftlet.net.ConnectionVetoException)

Aggregations

Connection (com.swiftmq.swiftlet.net.Connection)6 ConnectionManager (com.swiftmq.swiftlet.net.ConnectionManager)4 MQTTConnection (com.swiftmq.impl.mqtt.connection.MQTTConnection)3 SwiftletException (com.swiftmq.swiftlet.SwiftletException)3 ConnectionVetoException (com.swiftmq.swiftlet.net.ConnectionVetoException)3 Semaphore (com.swiftmq.tools.concurrent.Semaphore)2 SASLProvider (com.swiftmq.impl.amqp.sasl.provider.SASLProvider)1 Entity (com.swiftmq.mgmt.Entity)1 EntityRemoveException (com.swiftmq.mgmt.EntityRemoveException)1 Property (com.swiftmq.mgmt.Property)1