Search in sources :

Example 51 with RemotingConnection

use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.

the class AmqpSendReceiveInterceptorTest method testCreateQueueReceiver.

@Test(timeout = 60000)
public void testCreateQueueReceiver() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    server.getRemotingService().addIncomingInterceptor(new AmqpInterceptor() {

        @Override
        public boolean intercept(AMQPMessage message, RemotingConnection connection) throws ActiveMQException {
            latch.countDown();
            return true;
        }
    });
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    AmqpSender sender = session.createSender(getTestName());
    AmqpMessage message = new AmqpMessage();
    message.setMessageId("msg" + 1);
    message.setText("Test-Message");
    sender.send(message);
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    final CountDownLatch latch2 = new CountDownLatch(1);
    server.getRemotingService().addOutgoingInterceptor(new AmqpInterceptor() {

        @Override
        public boolean intercept(AMQPMessage packet, RemotingConnection connection) throws ActiveMQException {
            latch2.countDown();
            return true;
        }
    });
    AmqpReceiver receiver = session.createReceiver(getTestName());
    receiver.flow(2);
    AmqpMessage amqpMessage = receiver.receive(5, TimeUnit.SECONDS);
    assertNotNull(amqpMessage);
    assertEquals(latch2.getCount(), 0);
    sender.close();
    receiver.close();
    connection.close();
}
Also used : AmqpInterceptor(org.apache.activemq.artemis.protocol.amqp.broker.AmqpInterceptor) AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) AmqpReceiver(org.apache.activemq.transport.amqp.client.AmqpReceiver) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) CountDownLatch(java.util.concurrent.CountDownLatch) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) AMQPMessage(org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Test(org.junit.Test)

Example 52 with RemotingConnection

use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.

the class AmqpSendReceiveInterceptorTest method testCheckInterceptedMessageProperties.

@Test(timeout = 60000)
public void testCheckInterceptedMessageProperties() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final String addressQueue = getTestName();
    final String messageId = "lala200";
    final String correlationId = "lala-corrId";
    final String msgText = "Test intercepted message";
    final boolean durableMsg = false;
    final short priority = 8;
    final long timeToLive = 10000;
    final String replyTo = "reply-to-myQueue";
    Map<String, Object> expectedProperties = new HashMap<>();
    expectedProperties.put(ADDRESS, addressQueue);
    expectedProperties.put(MESSAGE_ID, messageId);
    expectedProperties.put(CORRELATION_ID, correlationId);
    expectedProperties.put(MESSAGE_TEXT, msgText);
    expectedProperties.put(DURABLE, durableMsg);
    expectedProperties.put(PRIORITY, priority);
    expectedProperties.put(REPLY_TO, replyTo);
    expectedProperties.put(TIME_TO_LIVE, timeToLive);
    server.getRemotingService().addIncomingInterceptor(new AmqpInterceptor() {

        @Override
        public boolean intercept(AMQPMessage message, RemotingConnection connection) throws ActiveMQException {
            latch.countDown();
            return checkMessageProperties(message, expectedProperties);
        }
    });
    AmqpClient client = createAmqpClient();
    AmqpConnection connection = addConnection(client.connect());
    AmqpSession session = connection.createSession();
    AmqpSender sender = session.createSender(getTestName());
    AmqpMessage message = new AmqpMessage();
    message.setMessageId(messageId);
    message.setCorrelationId(correlationId);
    message.setText(msgText);
    message.setDurable(durableMsg);
    message.setPriority(priority);
    message.setReplyToAddress(replyTo);
    message.setTimeToLive(timeToLive);
    sender.send(message);
    assertTrue(latch.await(2, TimeUnit.SECONDS));
    final CountDownLatch latch2 = new CountDownLatch(1);
    server.getRemotingService().addOutgoingInterceptor(new AmqpInterceptor() {

        @Override
        public boolean intercept(AMQPMessage packet, RemotingConnection connection) throws ActiveMQException {
            latch2.countDown();
            return checkMessageProperties(packet, expectedProperties);
        }
    });
    AmqpReceiver receiver = session.createReceiver(getTestName());
    receiver.flow(2);
    AmqpMessage amqpMessage = receiver.receive(5, TimeUnit.SECONDS);
    assertNotNull(amqpMessage);
    assertEquals(latch2.getCount(), 0);
    sender.close();
    receiver.close();
    connection.close();
}
Also used : AmqpConnection(org.apache.activemq.transport.amqp.client.AmqpConnection) HashMap(java.util.HashMap) AmqpReceiver(org.apache.activemq.transport.amqp.client.AmqpReceiver) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) AmqpClient(org.apache.activemq.transport.amqp.client.AmqpClient) CountDownLatch(java.util.concurrent.CountDownLatch) AmqpSender(org.apache.activemq.transport.amqp.client.AmqpSender) AMQPMessage(org.apache.activemq.artemis.protocol.amqp.broker.AMQPMessage) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) AmqpInterceptor(org.apache.activemq.artemis.protocol.amqp.broker.AmqpInterceptor) AmqpSession(org.apache.activemq.transport.amqp.client.AmqpSession) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Test(org.junit.Test)

Example 53 with RemotingConnection

use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.

the class AmqpOutboundConnectionTest method runOutboundConnectionTest.

private void runOutboundConnectionTest(boolean withSecurity, boolean closeFromClient) throws Exception {
    final ActiveMQServer remote;
    try {
        securityEnabled = withSecurity;
        remote = createServer(AMQP_PORT + 1);
    } finally {
        securityEnabled = false;
    }
    Wait.assertTrue(remote::isActive);
    final Map<String, Object> config = new LinkedHashMap<>();
    config.put(TransportConstants.HOST_PROP_NAME, "localhost");
    config.put(TransportConstants.PORT_PROP_NAME, String.valueOf(AMQP_PORT + 1));
    final ClientSASLFactory clientSASLFactory;
    if (withSecurity) {
        clientSASLFactory = availableMechanims -> {
            if (availableMechanims != null && Arrays.asList(availableMechanims).contains("PLAIN")) {
                return new PlainSASLMechanism(fullUser, fullPass);
            } else {
                return null;
            }
        };
    } else {
        clientSASLFactory = null;
    }
    final AtomicBoolean connectionOpened = new AtomicBoolean();
    EventHandler eventHandler = new EventHandler() {

        @Override
        public void onRemoteOpen(Connection connection) throws Exception {
            connectionOpened.set(true);
        }
    };
    ProtonClientConnectionManager lifeCycleListener = new ProtonClientConnectionManager(new AMQPClientConnectionFactory(server, "myid", Collections.singletonMap(Symbol.getSymbol("myprop"), "propvalue"), 5000), Optional.of(eventHandler), clientSASLFactory);
    ProtonClientProtocolManager protocolManager = new ProtonClientProtocolManager(new ProtonProtocolManagerFactory(), server);
    NettyConnector connector = new NettyConnector(config, lifeCycleListener, lifeCycleListener, server.getExecutorFactory().getExecutor(), server.getExecutorFactory().getExecutor(), server.getScheduledPool(), protocolManager);
    connector.start();
    Object connectionId = connector.createConnection().getID();
    assertNotNull(connectionId);
    RemotingConnection remotingConnection = lifeCycleListener.getConnection(connectionId);
    AtomicReference<ActiveMQException> ex = new AtomicReference<>();
    AtomicBoolean closed = new AtomicBoolean(false);
    remotingConnection.addCloseListener(() -> closed.set(true));
    remotingConnection.addFailureListener(new FailureListener() {

        @Override
        public void connectionFailed(ActiveMQException exception, boolean failedOver) {
            ex.set(exception);
        }

        @Override
        public void connectionFailed(ActiveMQException exception, boolean failedOver, String scaleDownTargetNodeID) {
            ex.set(exception);
        }
    });
    try {
        Wait.assertEquals(1, remote::getConnectionCount);
        Wait.assertTrue(connectionOpened::get);
        if (closeFromClient) {
            lifeCycleListener.stop();
        } else {
            remote.stop();
        }
        Wait.assertEquals(0, remote::getConnectionCount);
        assertTrue(remotingConnection.isDestroyed());
        if (!closeFromClient) {
            assertTrue(ex.get() instanceof ActiveMQRemoteDisconnectException);
        } else {
            assertNull(ex.get());
        }
    } finally {
        if (closeFromClient) {
            remote.stop();
        } else {
            lifeCycleListener.stop();
        }
    }
}
Also used : ProtonProtocolManagerFactory(org.apache.activemq.artemis.protocol.amqp.broker.ProtonProtocolManagerFactory) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) EventHandler(org.apache.activemq.artemis.protocol.amqp.proton.handler.EventHandler) ProtonClientProtocolManager(org.apache.activemq.artemis.protocol.amqp.client.ProtonClientProtocolManager) LinkedHashMap(java.util.LinkedHashMap) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSASLFactory(org.apache.activemq.artemis.protocol.amqp.sasl.ClientSASLFactory) ActiveMQRemoteDisconnectException(org.apache.activemq.artemis.api.core.ActiveMQRemoteDisconnectException) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(org.apache.qpid.proton.engine.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProtonClientConnectionManager(org.apache.activemq.artemis.protocol.amqp.client.ProtonClientConnectionManager) NettyConnector(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AMQPClientConnectionFactory(org.apache.activemq.artemis.protocol.amqp.client.AMQPClientConnectionFactory) FailureListener(org.apache.activemq.artemis.core.remoting.FailureListener)

Example 54 with RemotingConnection

use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.

the class ActiveMQServerControlImpl method closeConnectionsForAddress.

@Override
public boolean closeConnectionsForAddress(final String ipAddress) {
    checkStarted();
    clearIO();
    try {
        boolean closed = false;
        Set<RemotingConnection> connections = remotingService.getConnections();
        for (RemotingConnection connection : connections) {
            String remoteAddress = connection.getRemoteAddress();
            if (remoteAddress.contains(ipAddress)) {
                connection.fail(ActiveMQMessageBundle.BUNDLE.connectionsClosedByManagement(ipAddress));
                remotingService.removeConnection(connection.getID());
                closed = true;
            }
        }
        return closed;
    } finally {
        blockOnIO();
    }
}
Also used : RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString)

Example 55 with RemotingConnection

use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.

the class ActiveMQServerControlImpl method closeConsumerConnectionsForAddress.

@Override
public boolean closeConsumerConnectionsForAddress(final String address) {
    boolean closed = false;
    checkStarted();
    clearIO();
    try {
        for (Binding binding : postOffice.getMatchingBindings(SimpleString.toSimpleString(address)).getBindings()) {
            if (binding instanceof LocalQueueBinding) {
                Queue queue = ((LocalQueueBinding) binding).getQueue();
                for (Consumer consumer : queue.getConsumers()) {
                    if (consumer instanceof ServerConsumer) {
                        ServerConsumer serverConsumer = (ServerConsumer) consumer;
                        RemotingConnection connection = null;
                        for (RemotingConnection potentialConnection : remotingService.getConnections()) {
                            if (potentialConnection.getID().toString().equals(serverConsumer.getConnectionID())) {
                                connection = potentialConnection;
                            }
                        }
                        if (connection != null) {
                            remotingService.removeConnection(connection.getID());
                            connection.fail(ActiveMQMessageBundle.BUNDLE.consumerConnectionsClosedByManagement(address));
                            closed = true;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        ActiveMQServerLogger.LOGGER.failedToCloseConsumerConnectionsForAddress(address, e);
    } finally {
        blockOnIO();
    }
    return closed;
}
Also used : Binding(org.apache.activemq.artemis.core.postoffice.Binding) LocalQueueBinding(org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding) LocalQueueBinding(org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding) Consumer(org.apache.activemq.artemis.core.server.Consumer) ServerConsumer(org.apache.activemq.artemis.core.server.ServerConsumer) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ServerConsumer(org.apache.activemq.artemis.core.server.ServerConsumer) Queue(org.apache.activemq.artemis.core.server.Queue) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ListenerNotFoundException(javax.management.ListenerNotFoundException) ActiveMQAddressDoesNotExistException(org.apache.activemq.artemis.api.core.ActiveMQAddressDoesNotExistException)

Aggregations

RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)84 Test (org.junit.Test)44 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)36 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)31 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)30 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)22 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)21 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)17 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)16 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)16 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)15 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)14 CoreRemotingConnection (org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection)13 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)12 Connection (javax.jms.Connection)11 Interceptor (org.apache.activemq.artemis.api.core.Interceptor)11 Packet (org.apache.activemq.artemis.core.protocol.core.Packet)11 Session (javax.jms.Session)10