Search in sources :

Example 1 with NettyConnection

use of org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection in project activemq-artemis by apache.

the class CertificateUtil method getPeerPrincipalFromConnection.

public static Principal getPeerPrincipalFromConnection(RemotingConnection remotingConnection) {
    Principal result = null;
    if (remotingConnection != null) {
        Connection transportConnection = remotingConnection.getTransportConnection();
        if (transportConnection instanceof NettyConnection) {
            NettyConnection nettyConnection = (NettyConnection) transportConnection;
            ChannelHandler channelHandler = nettyConnection.getChannel().pipeline().get("ssl");
            if (channelHandler != null && channelHandler instanceof SslHandler) {
                SslHandler sslHandler = (SslHandler) channelHandler;
                try {
                    result = sslHandler.engine().getSession().getPeerPrincipal();
                } catch (SSLPeerUnverifiedException ignored) {
                }
            }
        }
    }
    return result;
}
Also used : NettyConnection(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) Connection(org.apache.activemq.artemis.spi.core.remoting.Connection) NettyConnection(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ChannelHandler(io.netty.channel.ChannelHandler) Principal(java.security.Principal) SslHandler(io.netty.handler.ssl.SslHandler)

Example 2 with NettyConnection

use of org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection in project activemq-artemis by apache.

the class ConsumerStuckTest method testClientStuckTestWithDirectDelivery.

@Test
public void testClientStuckTestWithDirectDelivery() throws Exception {
    ServerLocator locator = createNettyNonHALocator().setConnectionTTL(1000).setClientFailureCheckPeriod(100).setConsumerWindowSize(10 * 1024 * 1024).setCallTimeout(1000);
    ClientSessionFactory sf = locator.createSessionFactory();
    ((ClientSessionFactoryImpl) sf).stopPingingAfterOne();
    RemotingConnectionImpl remotingConnection = (RemotingConnectionImpl) sf.getConnection();
    ClientSession session = sf.createSession(false, true, true, true);
    session.createQueue(QUEUE, QUEUE, null, false);
    final int numMessages = 10000;
    final ClientConsumer consumer = session.createConsumer(QUEUE);
    session.start();
    final NettyConnection nettyConnection = (NettyConnection) remotingConnection.getTransportConnection();
    Thread tReceive = new Thread() {

        @Override
        public void run() {
            boolean first = true;
            try {
                while (!Thread.interrupted()) {
                    ClientMessage received = consumer.receive(500);
                    System.out.println("Received " + received);
                    if (first) {
                        first = false;
                        nettyConnection.getNettyChannel().config().setAutoRead(false);
                    }
                    if (received != null) {
                        received.acknowledge();
                    }
                }
            } catch (Throwable e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }
        }
    };
    tReceive.start();
    Thread sender = new Thread() {

        @Override
        public void run() {
            try (ServerLocator locator = createNettyNonHALocator();
                ClientSessionFactory factory = locator.createSessionFactory();
                ClientSession session = factory.createSession(false, true, true, true);
                ClientProducer producer = session.createProducer(QUEUE)) {
                for (int i = 0; i < numMessages; i++) {
                    ClientMessage message = createTextMessage(session, "m" + i);
                    producer.send(message);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    sender.start();
    try {
        long timeout = System.currentTimeMillis() + 20000;
        while (System.currentTimeMillis() < timeout && server.getSessions().size() != 2) {
            Thread.sleep(10);
        }
        assertEquals(2, server.getSessions().size());
        System.out.println("sessions = " + server.getSessions().size());
        assertEquals(2, server.getConnectionCount());
        timeout = System.currentTimeMillis() + 20000;
        while (System.currentTimeMillis() < timeout && server.getSessions().size() != 1) {
            Thread.sleep(10);
        }
        System.out.println("Size = " + server.getConnectionCount());
        System.out.println("sessions = " + server.getSessions().size());
        if (server.getSessions().size() != 1) {
            System.out.println(threadDump("Thread dump"));
            fail("The cleanup wasn't able to finish cleaning the session. It's probably stuck, look at the thread dump generated by the test for more information");
        }
        sender.join();
        timeout = System.currentTimeMillis() + 20000;
        while (System.currentTimeMillis() < timeout && server.getConnectionCount() != 0) {
            Thread.sleep(10);
        }
        assertEquals(0, server.getConnectionCount());
    } finally {
        nettyConnection.getNettyChannel().config().setAutoRead(true);
        tReceive.interrupt();
        tReceive.join();
    }
}
Also used : NettyConnection(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection) RemotingConnectionImpl(org.apache.activemq.artemis.core.protocol.core.impl.RemotingConnectionImpl) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientSessionFactoryImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 3 with NettyConnection

use of org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection in project activemq-artemis by apache.

the class NettyConnectionTest method testCreateBuffer.

@Test
public void testCreateBuffer() throws Exception {
    EmbeddedChannel channel = createChannel();
    NettyConnection conn = new NettyConnection(emptyMap, channel, new MyListener(), false, false);
    final int size = 1234;
    ActiveMQBuffer buff = conn.createTransportBuffer(size);
    // Netty buffer does lazy initialization.
    buff.writeByte((byte) 0x00);
    Assert.assertEquals(size, buff.capacity());
}
Also used : NettyConnection(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 4 with NettyConnection

use of org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection in project activemq-artemis by apache.

the class NettyConnectionTest method testGetID.

@Test
public void testGetID() throws Exception {
    Channel channel = createChannel();
    NettyConnection conn = new NettyConnection(emptyMap, channel, new MyListener(), false, false);
    Assert.assertEquals(channel.id(), conn.getID());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) NettyConnection(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection) Test(org.junit.Test)

Example 5 with NettyConnection

use of org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection in project activemq-artemis by apache.

the class NettyConnectionTest method throwsExceptionOnBlockUntilWritableIfClosed.

@Test(expected = IllegalStateException.class)
public void throwsExceptionOnBlockUntilWritableIfClosed() {
    EmbeddedChannel channel = createChannel();
    NettyConnection conn = new NettyConnection(emptyMap, channel, new MyListener(), false, false);
    conn.close();
    // to make sure the channel is closed it needs to run the pending tasks
    channel.runPendingTasks();
    conn.blockUntilWritable(0, 0, TimeUnit.NANOSECONDS);
}
Also used : NettyConnection(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Aggregations

NettyConnection (org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection)7 Test (org.junit.Test)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)2 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)2 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)2 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)2 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)2 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)2 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)2 ClientSessionFactoryImpl (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl)2 RemotingConnectionImpl (org.apache.activemq.artemis.core.protocol.core.impl.RemotingConnectionImpl)2 Channel (io.netty.channel.Channel)1 ChannelHandler (io.netty.channel.ChannelHandler)1 SslHandler (io.netty.handler.ssl.SslHandler)1 Principal (java.security.Principal)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)1 Connection (org.apache.activemq.artemis.spi.core.remoting.Connection)1