Search in sources :

Example 1 with SendAcknowledgementHandler

use of org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler in project activemq-artemis by apache.

the class ActiveMQCrashTest method testHang.

@Test
public void testHang() throws Exception {
    Configuration configuration = createDefaultInVMConfig().setPersistenceEnabled(false);
    server = addServer(ActiveMQServers.newActiveMQServer(configuration));
    server.start();
    server.getRemotingService().addIncomingInterceptor(new AckInterceptor(server));
    // Force an ack at once - this means the send call will block
    locator.setConfirmationWindowSize(1);
    ClientSessionFactory clientSessionFactory = createSessionFactory(locator);
    ClientSession session = clientSessionFactory.createSession();
    session.setSendAcknowledgementHandler(new SendAcknowledgementHandler() {

        @Override
        public void sendAcknowledged(final Message message) {
            ackReceived = true;
        }
    });
    ClientProducer producer = session.createProducer("fooQueue");
    ClientMessage msg = session.createMessage(false);
    msg.putStringProperty("someKey", "someValue");
    producer.send(msg);
    Thread.sleep(250);
    Assert.assertFalse(ackReceived);
    session.close();
}
Also used : Configuration(org.apache.activemq.artemis.core.config.Configuration) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) SendAcknowledgementHandler(org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 2 with SendAcknowledgementHandler

use of org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler in project activemq-artemis by apache.

the class ClientProducerImpl method largeMessageSendServer.

/**
 * Used to send serverMessages through the bridges. No need to validate compression here since
 * the message is only compressed at the client
 *
 * @param sendBlocking
 * @param msgI
 * @param handler
 * @throws ActiveMQException
 */
private void largeMessageSendServer(final boolean sendBlocking, final ICoreMessage msgI, final ClientProducerCredits credits, SendAcknowledgementHandler handler) throws ActiveMQException {
    sendInitialLargeMessageHeader(msgI, credits);
    LargeBodyEncoder context = msgI.getBodyEncoder();
    final long bodySize = context.getLargeBodySize();
    context.open();
    try {
        for (long pos = 0; pos < bodySize; ) {
            final boolean lastChunk;
            final int chunkLength = (int) Math.min((bodySize - pos), minLargeMessageSize);
            final ActiveMQBuffer bodyBuffer = ActiveMQBuffers.fixedBuffer(chunkLength);
            context.encode(bodyBuffer, chunkLength);
            pos += chunkLength;
            lastChunk = pos >= bodySize;
            SendAcknowledgementHandler messageHandler = lastChunk ? handler : null;
            int creditsUsed = sessionContext.sendServerLargeMessageChunk(msgI, -1, sendBlocking, lastChunk, bodyBuffer.toByteBuffer().array(), messageHandler);
            credits.acquireCredits(creditsUsed);
        }
    } finally {
        context.close();
    }
}
Also used : LargeBodyEncoder(org.apache.activemq.artemis.core.message.LargeBodyEncoder) SendAcknowledgementHandler(org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer)

Example 3 with SendAcknowledgementHandler

use of org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler in project activemq-artemis by apache.

the class SendAckFailTest method testSend.

@Test
public void testSend() throws Exception {
    Process process = SpawnedVMSupport.spawnVM(SendAckFailTest.class.getName());
    ActiveMQServer server = null;
    try {
        HashSet<Integer> listSent = new HashSet<>();
        Thread t = null;
        {
            ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
            ServerLocator locator = factory.getServerLocator();
            locator.setConfirmationWindowSize(0).setInitialConnectAttempts(100).setRetryInterval(100).setBlockOnDurableSend(false).setReconnectAttempts(0);
            ClientSessionFactory sf = locator.createSessionFactory();
            ClientSession session = sf.createSession();
            session.createAddress(SimpleString.toSimpleString("T1"), RoutingType.ANYCAST, true);
            session.createQueue(SimpleString.toSimpleString("T1"), RoutingType.ANYCAST, SimpleString.toSimpleString("T1"), true);
            ClientProducer producer = session.createProducer("T1");
            session.setSendAcknowledgementHandler(new SendAcknowledgementHandler() {

                @Override
                public void sendAcknowledged(Message message) {
                    listSent.add(message.getIntProperty("myid"));
                }
            });
            t = new Thread() {

                @Override
                public void run() {
                    for (int i = 0; i < 5000; i++) {
                        try {
                            producer.send(session.createMessage(true).putIntProperty("myid", i));
                        } catch (Exception e) {
                            e.printStackTrace();
                            break;
                        }
                    }
                }
            };
            t.start();
        }
        Wait.waitFor(() -> listSent.size() > 100, 5000, 10);
        Assert.assertTrue(process.waitFor(1, TimeUnit.MINUTES));
        server = startServer(false);
        {
            ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
            ServerLocator locator = factory.getServerLocator();
            ClientSessionFactory sf = locator.createSessionFactory();
            ClientSession session = sf.createSession();
            ClientConsumer consumer = session.createConsumer("T1");
            session.start();
            for (int i = 0; i < listSent.size(); i++) {
                ClientMessage message = consumer.receive(1000);
                if (message == null) {
                    for (Integer msgi : listSent) {
                        System.out.println("Message " + msgi + " was lost");
                    }
                    fail("missed messages!");
                }
                message.acknowledge();
                if (!listSent.remove(message.getIntProperty("myid"))) {
                    System.out.println("Message " + message + " with id " + message.getIntProperty("myid") + " received in duplicate");
                    fail("Message " + message + " with id " + message.getIntProperty("myid") + " received in duplicate");
                }
            }
        }
    } finally {
        if (process != null) {
            process.destroy();
        }
        if (server != null) {
            server.stop();
        }
    }
}
Also used : LargeServerMessage(org.apache.activemq.artemis.core.server.LargeServerMessage) PagedMessage(org.apache.activemq.artemis.core.paging.PagedMessage) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) SendAcknowledgementHandler(org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with SendAcknowledgementHandler

use of org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler in project activemq-artemis by apache.

the class SendAcknowledgementsExample method main.

public static void main(final String[] args) throws Exception {
    Connection connection = null;
    InitialContext initialContext = null;
    try {
        // Step 1. Create an initial context to perform the JNDI lookup.
        initialContext = new InitialContext();
        // Step 2. Perfom a lookup on the queue
        Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
        // Step 3. Perform a lookup on the Connection Factory
        ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
        // Step 4. Create a JMS Connection
        connection = cf.createConnection();
        // Step 5. Define a SendAcknowledgementHandler which will receive asynchronous acknowledgements
        class MySendAcknowledgementsHandler implements SendAcknowledgementHandler {

            int count = 0;

            @Override
            public void sendAcknowledged(final Message message) {
                System.out.println("Received send acknowledgement for message " + count++);
            }
        }
        // Step 6. Create a JMS Session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Step 7. Set the handler on the underlying core session
        ClientSession coreSession = ((ActiveMQSession) session).getCoreSession();
        coreSession.setSendAcknowledgementHandler(new MySendAcknowledgementsHandler());
        // Step 6. Create a JMS Message Producer
        MessageProducer producer = session.createProducer(queue);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        // Step 7. Send 5000 messages, the handler will get called asynchronously some time later after the messages
        // are sent.
        final int numMessages = 5000;
        for (int i = 0; i < numMessages; i++) {
            javax.jms.Message jmsMessage = session.createMessage();
            producer.send(jmsMessage);
            System.out.println("Sent message " + i);
        }
    } finally {
        // Step 12. Be sure to close our JMS resources!
        if (initialContext != null) {
            initialContext.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : Message(org.apache.activemq.artemis.api.core.Message) Connection(javax.jms.Connection) SendAcknowledgementHandler(org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler) InitialContext(javax.naming.InitialContext) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) Session(javax.jms.Session) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession)

Example 5 with SendAcknowledgementHandler

use of org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler in project activemq-artemis by apache.

the class SessionSendAcknowledgementHandlerTest method testSetInvalidSendACK.

@Test
public void testSetInvalidSendACK() throws Exception {
    ServerLocator locator = createInVMNonHALocator();
    locator.setConfirmationWindowSize(-1);
    ClientSessionFactory csf = createSessionFactory(locator);
    ClientSession session = csf.createSession(null, null, false, true, true, false, 1);
    boolean failed = false;
    try {
        session.setSendAcknowledgementHandler(new SendAcknowledgementHandler() {

            @Override
            public void sendAcknowledged(Message message) {
            }
        });
    } catch (Throwable expected) {
        failed = true;
    }
    assertTrue("Expected a failure on setting ACK Handler", failed);
    session.createQueue(address, queueName, false);
}
Also used : ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) SendAcknowledgementHandler(org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Aggregations

SendAcknowledgementHandler (org.apache.activemq.artemis.api.core.client.SendAcknowledgementHandler)5 Message (org.apache.activemq.artemis.api.core.Message)4 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)4 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)3 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)3 Test (org.junit.Test)3 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)2 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)2 HashSet (java.util.HashSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Connection (javax.jms.Connection)1 ConnectionFactory (javax.jms.ConnectionFactory)1 MessageProducer (javax.jms.MessageProducer)1 Queue (javax.jms.Queue)1 Session (javax.jms.Session)1 InitialContext (javax.naming.InitialContext)1 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)1 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)1 Configuration (org.apache.activemq.artemis.core.config.Configuration)1 LargeBodyEncoder (org.apache.activemq.artemis.core.message.LargeBodyEncoder)1