Search in sources :

Example 1 with ServerSessionImpl

use of org.apache.activemq.artemis.core.server.impl.ServerSessionImpl in project activemq-artemis by apache.

the class MQTTConnectionManager method createServerSession.

/**
 * Creates an internal Server Session.
 *
 * @param username
 * @param password
 * @return
 * @throws Exception
 */
ServerSessionImpl createServerSession(String username, String password) throws Exception {
    String id = UUIDGenerator.getInstance().generateStringUUID();
    ActiveMQServer server = session.getServer();
    ServerSession serverSession = server.createSession(id, username, password, ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, session.getConnection(), MQTTUtil.SESSION_AUTO_COMMIT_SENDS, MQTTUtil.SESSION_AUTO_COMMIT_ACKS, MQTTUtil.SESSION_PREACKNOWLEDGE, MQTTUtil.SESSION_XA, null, session.getSessionCallback(), MQTTUtil.SESSION_AUTO_CREATE_QUEUE, server.newOperationContext(), session.getProtocolManager().getPrefixes());
    return (ServerSessionImpl) serverSession;
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) ServerSessionImpl(org.apache.activemq.artemis.core.server.impl.ServerSessionImpl)

Example 2 with ServerSessionImpl

use of org.apache.activemq.artemis.core.server.impl.ServerSessionImpl in project activemq-artemis by apache.

the class TemporaryQueueTest method testBlockingWithTemporaryQueue.

@Test
public void testBlockingWithTemporaryQueue() throws Exception {
    AddressSettings setting = new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.BLOCK).setMaxSizeBytes(1024 * 1024);
    server.getAddressSettingsRepository().addMatch("TestAD", setting);
    ClientSessionFactory consumerCF = createSessionFactory(locator);
    ClientSession consumerSession = consumerCF.createSession(true, true);
    consumerSession.addMetaData("consumer", "consumer");
    consumerSession.createTemporaryQueue("TestAD", "Q1");
    consumerSession.createConsumer("Q1");
    consumerSession.start();
    final ClientProducerImpl prod = (ClientProducerImpl) session.createProducer("TestAD");
    final AtomicInteger errors = new AtomicInteger(0);
    final AtomicInteger msgs = new AtomicInteger(0);
    final int TOTAL_MSG = 1000;
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                for (int i = 0; i < TOTAL_MSG; i++) {
                    ClientMessage msg = session.createMessage(false);
                    msg.getBodyBuffer().writeBytes(new byte[1024]);
                    prod.send(msg);
                    msgs.incrementAndGet();
                }
            } catch (Throwable e) {
                e.printStackTrace();
                errors.incrementAndGet();
            }
            System.out.println("done");
        }
    };
    t.start();
    while (msgs.get() == 0) {
        Thread.sleep(100);
    }
    int blockedTime = 0;
    // https://issues.apache.org/jira/browse/ARTEMIS-368
    while (t.isAlive() && errors.get() == 0 && (!prod.getProducerCredits().isBlocked() || blockedTime < 60)) {
        if (prod.getProducerCredits().isBlocked()) {
            blockedTime++;
        } else {
            blockedTime = 0;
        }
        Thread.sleep(100);
    }
    assertEquals(0, errors.get());
    ClientSessionFactory newConsumerCF = createSessionFactory(locator);
    ClientSession newConsumerSession = newConsumerCF.createSession(true, true);
    newConsumerSession.createTemporaryQueue("TestAD", "Q2");
    ClientConsumer newConsumer = newConsumerSession.createConsumer("Q2");
    newConsumerSession.start();
    int toReceive = TOTAL_MSG - msgs.get();
    for (ServerSession sessionIterator : server.getSessions()) {
        if (sessionIterator.getMetaData("consumer") != null) {
            System.out.println("Failing session");
            ServerSessionImpl impl = (ServerSessionImpl) sessionIterator;
            impl.getRemotingConnection().fail(new ActiveMQDisconnectedException("failure e"));
        }
    }
    int secondReceive = 0;
    ClientMessage msg = null;
    while (secondReceive < toReceive && (msg = newConsumer.receive(5000)) != null) {
        msg.acknowledge();
        secondReceive++;
    }
    assertNull(newConsumer.receiveImmediate());
    assertEquals(toReceive, secondReceive);
    t.join();
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) ActiveMQDisconnectedException(org.apache.activemq.artemis.api.core.ActiveMQDisconnectedException) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducerImpl(org.apache.activemq.artemis.core.client.impl.ClientProducerImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ServerSessionImpl(org.apache.activemq.artemis.core.server.impl.ServerSessionImpl) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Test(org.junit.Test)

Example 3 with ServerSessionImpl

use of org.apache.activemq.artemis.core.server.impl.ServerSessionImpl in project activemq-artemis by apache.

the class MQTTConnectionManager method connect.

/**
 * Handles the connect packet.  See spec for details on each of parameters.
 */
synchronized void connect(String cId, String username, byte[] passwordInBytes, boolean will, byte[] willMessage, String willTopic, boolean willRetain, int willQosLevel, boolean cleanSession) throws Exception {
    String clientId = validateClientId(cId, cleanSession);
    if (clientId == null) {
        session.getProtocolHandler().sendConnack(MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED);
        session.getProtocolHandler().disconnect(true);
        return;
    }
    session.setSessionState(getSessionState(clientId));
    String password = passwordInBytes == null ? null : new String(passwordInBytes, CharsetUtil.UTF_8);
    session.getConnection().setClientID(clientId);
    ServerSessionImpl serverSession = createServerSession(username, password);
    serverSession.start();
    session.setServerSession(serverSession);
    session.setIsClean(cleanSession);
    if (will) {
        isWill = true;
        this.willMessage = ByteBufAllocator.DEFAULT.buffer(willMessage.length);
        this.willMessage.writeBytes(willMessage);
        this.willQoSLevel = willQosLevel;
        this.willRetain = willRetain;
        this.willTopic = willTopic;
    }
    session.getConnection().setConnected(true);
    session.start();
    session.getProtocolHandler().sendConnack(MqttConnectReturnCode.CONNECTION_ACCEPTED);
}
Also used : ServerSessionImpl(org.apache.activemq.artemis.core.server.impl.ServerSessionImpl)

Example 4 with ServerSessionImpl

use of org.apache.activemq.artemis.core.server.impl.ServerSessionImpl in project activemq-artemis by apache.

the class MQTTPublishManager method stop.

synchronized void stop() throws Exception {
    ServerSessionImpl serversession = session.getServerSession();
    if (serversession != null) {
        serversession.removeProducer(serversession.getName());
    }
    if (managementConsumer != null) {
        managementConsumer.removeItself();
        managementConsumer.setStarted(false);
        managementConsumer.close(false);
    }
}
Also used : ServerSessionImpl(org.apache.activemq.artemis.core.server.impl.ServerSessionImpl)

Example 5 with ServerSessionImpl

use of org.apache.activemq.artemis.core.server.impl.ServerSessionImpl in project activemq-artemis by apache.

the class StompSession method sendInternalLarge.

public void sendInternalLarge(CoreMessage message, boolean direct) throws Exception {
    int headerSize = message.getHeadersAndPropertiesEncodeSize();
    if (headerSize >= connection.getMinLargeMessageSize()) {
        throw BUNDLE.headerTooBig();
    }
    StorageManager storageManager = ((ServerSessionImpl) session).getStorageManager();
    long id = storageManager.generateID();
    LargeServerMessage largeMessage = storageManager.createLargeMessage(id, message);
    ActiveMQBuffer body = message.getReadOnlyBodyBuffer();
    byte[] bytes = new byte[body.readableBytes()];
    body.readBytes(bytes);
    largeMessage.addBytes(bytes);
    largeMessage.releaseResources();
    largeMessage.putLongProperty(Message.HDR_LARGE_BODY_SIZE, bytes.length);
    session.send(largeMessage, direct);
    largeMessage = null;
}
Also used : StorageManager(org.apache.activemq.artemis.core.persistence.StorageManager) ServerSessionImpl(org.apache.activemq.artemis.core.server.impl.ServerSessionImpl) LargeServerMessage(org.apache.activemq.artemis.core.server.LargeServerMessage) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer)

Aggregations

ServerSessionImpl (org.apache.activemq.artemis.core.server.impl.ServerSessionImpl)5 ServerSession (org.apache.activemq.artemis.core.server.ServerSession)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)1 ActiveMQDisconnectedException (org.apache.activemq.artemis.api.core.ActiveMQDisconnectedException)1 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)1 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)1 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)1 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)1 ClientProducerImpl (org.apache.activemq.artemis.core.client.impl.ClientProducerImpl)1 StorageManager (org.apache.activemq.artemis.core.persistence.StorageManager)1 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)1 LargeServerMessage (org.apache.activemq.artemis.core.server.LargeServerMessage)1 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)1 Test (org.junit.Test)1