Search in sources :

Example 11 with OperationContext

use of org.apache.activemq.artemis.core.persistence.OperationContext in project activemq-artemis by apache.

the class StompPluginTest method createServer.

@Override
protected JMSServerManager createServer() throws Exception {
    JMSServerManager server = super.createServer();
    server.getActiveMQServer().registerBrokerPlugin(verifier);
    server.getActiveMQServer().registerBrokerPlugin(new ActiveMQServerPlugin() {

        @Override
        public void beforeCreateSession(String name, String username, int minLargeMessageSize, RemotingConnection connection, boolean autoCommitSends, boolean autoCommitAcks, boolean preAcknowledge, boolean xa, String defaultAddress, SessionCallback callback, boolean autoCreateQueues, OperationContext context, Map<SimpleString, RoutingType> prefixes) throws ActiveMQException {
            if (connection instanceof StompConnection) {
                stompBeforeCreateSession.set(true);
            }
        }

        @Override
        public void beforeCloseSession(ServerSession session, boolean failed) throws ActiveMQException {
            if (session.getRemotingConnection() instanceof StompConnection) {
                stompBeforeRemoveSession.set(true);
            }
        }
    });
    return server;
}
Also used : OperationContext(org.apache.activemq.artemis.core.persistence.OperationContext) JMSServerManager(org.apache.activemq.artemis.jms.server.JMSServerManager) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SessionCallback(org.apache.activemq.artemis.spi.core.protocol.SessionCallback) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQServerPlugin(org.apache.activemq.artemis.core.server.plugin.ActiveMQServerPlugin) StompConnection(org.apache.activemq.artemis.core.protocol.stomp.StompConnection) RoutingType(org.apache.activemq.artemis.api.core.RoutingType)

Example 12 with OperationContext

use of org.apache.activemq.artemis.core.persistence.OperationContext in project activemq-artemis by apache.

the class ActiveMQPacketHandler method handleCreateSession.

private void handleCreateSession(final CreateSessionMessage request) {
    boolean incompatibleVersion = false;
    Packet response;
    try {
        Version version = server.getVersion();
        if (!version.isCompatible(request.getVersion())) {
            throw ActiveMQMessageBundle.BUNDLE.incompatibleClientServer();
        }
        if (!server.isStarted()) {
            throw ActiveMQMessageBundle.BUNDLE.serverNotStarted();
        }
        if (connection.getChannelVersion() == 0) {
            connection.setChannelVersion(request.getVersion());
        } else if (connection.getChannelVersion() != request.getVersion()) {
            ActiveMQServerLogger.LOGGER.incompatibleVersionAfterConnect(request.getVersion(), connection.getChannelVersion());
        }
        Channel channel = connection.getChannel(request.getSessionChannelID(), request.getWindowSize());
        ActiveMQPrincipal activeMQPrincipal = null;
        if (request.getUsername() == null) {
            activeMQPrincipal = connection.getDefaultActiveMQPrincipal();
        }
        OperationContext sessionOperationContext = server.newOperationContext();
        Map<SimpleString, RoutingType> routingTypeMap = protocolManager.getPrefixes();
        CoreSessionCallback sessionCallback = new CoreSessionCallback(request.getName(), protocolManager, channel, connection);
        ServerSession session = server.createSession(request.getName(), activeMQPrincipal == null ? request.getUsername() : activeMQPrincipal.getUserName(), activeMQPrincipal == null ? request.getPassword() : activeMQPrincipal.getPassword(), request.getMinLargeMessageSize(), connection, request.isAutoCommitSends(), request.isAutoCommitAcks(), request.isPreAcknowledge(), request.isXA(), request.getDefaultAddress(), sessionCallback, true, sessionOperationContext, routingTypeMap);
        ServerProducer serverProducer = new ServerProducerImpl(session.getName(), "CORE", request.getDefaultAddress());
        session.addProducer(serverProducer);
        ServerSessionPacketHandler handler = new ServerSessionPacketHandler(server, protocolManager, session, server.getStorageManager(), channel);
        channel.setHandler(handler);
        sessionCallback.setSessionHandler(handler);
        // TODO - where is this removed?
        protocolManager.addSessionHandler(request.getName(), handler);
        response = new CreateSessionResponseMessage(server.getVersion().getIncrementingVersion());
    } catch (ActiveMQClusterSecurityException | ActiveMQSecurityException e) {
        ActiveMQServerLogger.LOGGER.securityProblemWhileCreatingSession(e.getMessage());
        response = new ActiveMQExceptionMessage(e);
    } catch (ActiveMQException e) {
        if (e.getType() == ActiveMQExceptionType.INCOMPATIBLE_CLIENT_SERVER_VERSIONS) {
            incompatibleVersion = true;
            logger.debug("Sending ActiveMQException after Incompatible client", e);
        } else {
            ActiveMQServerLogger.LOGGER.failedToCreateSession(e);
        }
        response = new ActiveMQExceptionMessage(e);
    } catch (Exception e) {
        ActiveMQServerLogger.LOGGER.failedToCreateSession(e);
        response = new ActiveMQExceptionMessage(new ActiveMQInternalErrorException());
    }
    // are not compatible
    if (incompatibleVersion) {
        channel1.sendAndFlush(response);
    } else {
        channel1.send(response);
    }
}
Also used : OperationContext(org.apache.activemq.artemis.core.persistence.OperationContext) ServerSessionPacketHandler(org.apache.activemq.artemis.core.protocol.core.ServerSessionPacketHandler) Packet(org.apache.activemq.artemis.core.protocol.core.Packet) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) ActiveMQPrincipal(org.apache.activemq.artemis.core.security.ActiveMQPrincipal) CreateSessionResponseMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.CreateSessionResponseMessage) Channel(org.apache.activemq.artemis.core.protocol.core.Channel) ActiveMQExceptionMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.ActiveMQExceptionMessage) ActiveMQClusterSecurityException(org.apache.activemq.artemis.api.core.ActiveMQClusterSecurityException) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException) ServerProducer(org.apache.activemq.artemis.core.server.ServerProducer) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQClusterSecurityException(org.apache.activemq.artemis.api.core.ActiveMQClusterSecurityException) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException) ActiveMQSecurityException(org.apache.activemq.artemis.api.core.ActiveMQSecurityException) ServerProducerImpl(org.apache.activemq.artemis.core.server.impl.ServerProducerImpl) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Version(org.apache.activemq.artemis.core.version.Version) ActiveMQSecurityException(org.apache.activemq.artemis.api.core.ActiveMQSecurityException) RoutingType(org.apache.activemq.artemis.api.core.RoutingType)

Example 13 with OperationContext

use of org.apache.activemq.artemis.core.persistence.OperationContext in project activemq-artemis by apache.

the class ReplicationTest method testExceptionSettingActionBefore.

@Test
public void testExceptionSettingActionBefore() throws Exception {
    OperationContext ctx = OperationContextImpl.getContext(factory);
    ctx.storeLineUp();
    String msg = "I'm an exception";
    ctx.onError(ActiveMQExceptionType.UNBLOCKED.getCode(), msg);
    final AtomicInteger lastError = new AtomicInteger(0);
    final List<String> msgsResult = new ArrayList<>();
    final CountDownLatch latch = new CountDownLatch(1);
    ctx.executeOnCompletion(new IOCallback() {

        @Override
        public void onError(final int errorCode, final String errorMessage) {
            lastError.set(errorCode);
            msgsResult.add(errorMessage);
            latch.countDown();
        }

        @Override
        public void done() {
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(5, lastError.get());
    Assert.assertEquals(1, msgsResult.size());
    Assert.assertEquals(msg, msgsResult.get(0));
    final CountDownLatch latch2 = new CountDownLatch(1);
    // Adding the Task after the exception should still throw an exception
    ctx.executeOnCompletion(new IOCallback() {

        @Override
        public void onError(final int errorCode, final String errorMessage) {
            lastError.set(errorCode);
            msgsResult.add(errorMessage);
            latch2.countDown();
        }

        @Override
        public void done() {
        }
    });
    Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(2, msgsResult.size());
    Assert.assertEquals(msg, msgsResult.get(0));
    Assert.assertEquals(msg, msgsResult.get(1));
    final CountDownLatch latch3 = new CountDownLatch(1);
    ctx.executeOnCompletion(new IOCallback() {

        @Override
        public void onError(final int errorCode, final String errorMessage) {
        }

        @Override
        public void done() {
            latch3.countDown();
        }
    });
    Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
}
Also used : OperationContext(org.apache.activemq.artemis.core.persistence.OperationContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CountDownLatch(java.util.concurrent.CountDownLatch) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) Test(org.junit.Test)

Example 14 with OperationContext

use of org.apache.activemq.artemis.core.persistence.OperationContext in project activemq-artemis by apache.

the class ReplicationTest method testOrderOnNonPersistency.

@Test
public void testOrderOnNonPersistency() throws Exception {
    setupServer(true);
    final ArrayList<Integer> executions = new ArrayList<>();
    StorageManager storage = getStorage();
    manager = liveServer.getReplicationManager();
    Journal replicatedJournal = new ReplicatedJournal((byte) 1, new FakeJournal(), manager);
    int numberOfAdds = 200;
    final CountDownLatch latch = new CountDownLatch(numberOfAdds);
    OperationContext ctx = storage.getContext();
    for (int i = 0; i < numberOfAdds; i++) {
        final int nAdd = i;
        if (i % 2 == 0) {
            replicatedJournal.appendPrepareRecord(i, new FakeData(), false);
        }
        ctx.executeOnCompletion(new IOCallback() {

            @Override
            public void onError(final int errorCode, final String errorMessage) {
            }

            @Override
            public void done() {
                executions.add(nAdd);
                latch.countDown();
            }
        });
    }
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    for (int i = 0; i < numberOfAdds; i++) {
        Assert.assertEquals(i, executions.get(i).intValue());
    }
    Assert.assertEquals(0, manager.getActiveTokens().size());
}
Also used : OperationContext(org.apache.activemq.artemis.core.persistence.OperationContext) ArrayList(java.util.ArrayList) JournalStorageManager(org.apache.activemq.artemis.core.persistence.impl.journal.JournalStorageManager) StorageManager(org.apache.activemq.artemis.core.persistence.StorageManager) ReplicatedJournal(org.apache.activemq.artemis.core.replication.ReplicatedJournal) Journal(org.apache.activemq.artemis.core.journal.Journal) ReplicatedJournal(org.apache.activemq.artemis.core.replication.ReplicatedJournal) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CountDownLatch(java.util.concurrent.CountDownLatch) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 15 with OperationContext

use of org.apache.activemq.artemis.core.persistence.OperationContext in project activemq-artemis by apache.

the class LocalGroupingHandler method propose.

@Override
public Response propose(final Proposal proposal) throws Exception {
    OperationContext originalCtx = storageManager.getContext();
    try {
        // the waitCompletion cannot be done inside an ordered executor or we would starve when the thread pool is full
        storageManager.setContext(storageManager.newSingleThreadContext());
        if (proposal.getClusterName() == null) {
            GroupBinding original = map.get(proposal.getGroupId());
            if (original != null) {
                original.use();
                return new Response(proposal.getGroupId(), original.getClusterName());
            } else {
                return null;
            }
        }
        boolean addRecord = false;
        GroupBinding groupBinding = null;
        lock.lock();
        try {
            groupBinding = map.get(proposal.getGroupId());
            if (groupBinding != null) {
                groupBinding.use();
                // Returning with an alternate cluster name, as it's been already grouped
                return new Response(groupBinding.getGroupId(), proposal.getClusterName(), groupBinding.getClusterName());
            } else {
                addRecord = true;
                groupBinding = new GroupBinding(proposal.getGroupId(), proposal.getClusterName());
                groupBinding.setId(storageManager.generateID());
                List<GroupBinding> newList = new ArrayList<>();
                List<GroupBinding> oldList = groupMap.putIfAbsent(groupBinding.getClusterName(), newList);
                if (oldList != null) {
                    newList = oldList;
                }
                newList.add(groupBinding);
                map.put(groupBinding.getGroupId(), groupBinding);
            }
        } finally {
            lock.unlock();
        }
        // Storing the record outside of any locks
        if (addRecord) {
            storageManager.addGrouping(groupBinding);
        }
        return new Response(groupBinding.getGroupId(), groupBinding.getClusterName());
    } finally {
        storageManager.setContext(originalCtx);
    }
}
Also used : OperationContext(org.apache.activemq.artemis.core.persistence.OperationContext) ArrayList(java.util.ArrayList)

Aggregations

OperationContext (org.apache.activemq.artemis.core.persistence.OperationContext)18 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)7 CountDownLatch (java.util.concurrent.CountDownLatch)5 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)5 ArrayList (java.util.ArrayList)4 RoutingType (org.apache.activemq.artemis.api.core.RoutingType)4 Test (org.junit.Test)4 Configuration (org.apache.activemq.artemis.core.config.Configuration)3 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)3 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)3 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)3 IOCallback (org.apache.activemq.artemis.core.io.IOCallback)3 PagingManager (org.apache.activemq.artemis.core.paging.PagingManager)3 PagingStore (org.apache.activemq.artemis.core.paging.PagingStore)3 ServerSession (org.apache.activemq.artemis.core.server.ServerSession)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ActiveMQSecurityException (org.apache.activemq.artemis.api.core.ActiveMQSecurityException)2 ReplicationManager (org.apache.activemq.artemis.core.replication.ReplicationManager)2 ActiveMQServerPlugin (org.apache.activemq.artemis.core.server.plugin.ActiveMQServerPlugin)2 ActiveMQAMQPResourceLimitExceededException (org.apache.activemq.artemis.protocol.amqp.exceptions.ActiveMQAMQPResourceLimitExceededException)2