Search in sources :

Example 1 with ActiveMQMessage

use of org.apache.activemq.artemis.jms.client.ActiveMQMessage in project activemq-artemis by apache.

the class ActiveMQMessageHandler method onMessage.

@Override
public void onMessage(final ClientMessage message) {
    if (logger.isTraceEnabled()) {
        logger.trace("onMessage(" + message + ")");
    }
    ActiveMQMessage msg = ActiveMQMessage.createMessage(message, session, options);
    boolean beforeDelivery = false;
    try {
        if (activation.getActivationSpec().getTransactionTimeout() > 0 && tm != null) {
            tm.setTransactionTimeout(activation.getActivationSpec().getTransactionTimeout());
        }
        if (logger.isTraceEnabled()) {
            logger.trace("HornetQMessageHandler::calling beforeDelivery on message " + message);
        }
        endpoint.beforeDelivery(ActiveMQActivation.ONMESSAGE);
        beforeDelivery = true;
        msg.doBeforeReceive();
        if (transacted) {
            message.individualAcknowledge();
        }
        ((MessageListener) endpoint).onMessage(msg);
        if (!transacted) {
            message.individualAcknowledge();
        }
        if (logger.isTraceEnabled()) {
            logger.trace("HornetQMessageHandler::calling afterDelivery on message " + message);
        }
        try {
            endpoint.afterDelivery();
        } catch (ResourceException e) {
            ActiveMQRALogger.LOGGER.unableToCallAfterDelivery(e);
            // If we get here, The TX was already rolled back
            // However we must do some stuff now to make sure the client message buffer is cleared
            // so we mark this as rollbackonly
            session.markRollbackOnly();
            return;
        }
        if (useLocalTx) {
            session.commit();
        }
        if (logger.isTraceEnabled()) {
            logger.trace("finished onMessage on " + message);
        }
    } catch (Throwable e) {
        ActiveMQRALogger.LOGGER.errorDeliveringMessage(e);
        // we need to call before/afterDelivery as a pair
        if (beforeDelivery) {
            if (useXA && tm != null) {
                // this is to avoid a scenario where afterDelivery would kick in
                try {
                    Transaction tx = tm.getTransaction();
                    if (tx != null) {
                        tx.setRollbackOnly();
                    }
                } catch (Exception e1) {
                    ActiveMQRALogger.LOGGER.unableToClearTheTransaction(e1);
                }
            }
            MessageEndpoint endToUse = endpoint;
            try {
                // to avoid a NPE that would happen while the RA is in tearDown
                if (endToUse != null) {
                    endToUse.afterDelivery();
                }
            } catch (ResourceException e1) {
                ActiveMQRALogger.LOGGER.unableToCallAfterDelivery(e1);
            }
        }
        if (useLocalTx || !activation.isDeliveryTransacted()) {
            try {
                session.rollback(true);
            } catch (ActiveMQException e1) {
                ActiveMQRALogger.LOGGER.unableToRollbackTX();
            }
        }
        // This is to make sure we will issue a rollback after failures
        // so that would cleanup consumer buffers among other things
        session.markRollbackOnly();
    } finally {
        try {
            session.resetIfNeeded();
        } catch (ActiveMQException e) {
            ActiveMQRALogger.LOGGER.unableToResetSession(activation.toString(), e);
            activation.startReconnectThread("Reset MessageHandler after Failure Thread");
        }
    }
}
Also used : MessageEndpoint(javax.resource.spi.endpoint.MessageEndpoint) Transaction(javax.transaction.Transaction) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) MessageListener(javax.jms.MessageListener) ResourceException(javax.resource.ResourceException) ResourceException(javax.resource.ResourceException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQMessage(org.apache.activemq.artemis.jms.client.ActiveMQMessage)

Example 2 with ActiveMQMessage

use of org.apache.activemq.artemis.jms.client.ActiveMQMessage in project activemq-artemis by apache.

the class TopicClusterTest method checkInternalProperty.

// check that the internal property is in the core
// but didn't exposed to jms
private void checkInternalProperty(Message... msgs) throws Exception {
    boolean checked = false;
    for (Message m : msgs) {
        ActiveMQMessage hqMessage = (ActiveMQMessage) m;
        ClientMessage coreMessage = hqMessage.getCoreMessage();
        Set<SimpleString> coreProps = coreMessage.getPropertyNames();
        System.out.println("core props: " + coreProps);
        boolean exist = false;
        for (SimpleString prop : coreProps) {
            if (prop.startsWith(org.apache.activemq.artemis.api.core.Message.HDR_ROUTE_TO_IDS)) {
                exist = true;
                break;
            }
        }
        if (exist) {
            Enumeration enumProps = m.getPropertyNames();
            while (enumProps.hasMoreElements()) {
                String propName = (String) enumProps.nextElement();
                assertFalse("Shouldn't be in jms property: " + propName, propName.startsWith(org.apache.activemq.artemis.api.core.Message.HDR_ROUTE_TO_IDS.toString()));
            }
            checked = true;
        }
    }
    assertTrue(checked);
}
Also used : Enumeration(java.util.Enumeration) TextMessage(javax.jms.TextMessage) ActiveMQMessage(org.apache.activemq.artemis.jms.client.ActiveMQMessage) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(javax.jms.Message) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQMessage(org.apache.activemq.artemis.jms.client.ActiveMQMessage)

Example 3 with ActiveMQMessage

use of org.apache.activemq.artemis.jms.client.ActiveMQMessage in project activemq-artemis by apache.

the class JMSBridgeTest method messageIDInHeader.

private void messageIDInHeader(final boolean on) throws Exception {
    JMSBridgeImpl bridge = null;
    Connection connSource = null;
    Connection connTarget = null;
    try {
        final int NUM_MESSAGES = 10;
        bridge = new JMSBridgeImpl(cff0, cff1, sourceQueueFactory, targetQueueFactory, null, null, null, null, null, 5000, 10, QualityOfServiceMode.AT_MOST_ONCE, 1, -1, null, null, on);
        bridge.start();
        connSource = cf0.createConnection();
        connTarget = cf1.createConnection();
        JMSBridgeTest.log.trace("Sending " + NUM_MESSAGES + " messages");
        List<String> ids1 = new ArrayList<>();
        Session sessSource = connSource.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer prod = sessSource.createProducer(sourceQueue);
        for (int i = 0; i < NUM_MESSAGES; i++) {
            TextMessage tm = sessSource.createTextMessage("message" + i);
            // We add some properties to make sure they get passed through ok
            tm.setStringProperty("wib", "uhuh");
            tm.setBooleanProperty("cheese", true);
            tm.setIntProperty("Sausages", 23);
            tm.setByteProperty("bacon", (byte) 12);
            tm.setDoubleProperty("toast", 17261762.12121d);
            tm.setFloatProperty("orange", 1212.1212f);
            tm.setLongProperty("blurg", 817217827L);
            tm.setShortProperty("stst", (short) 26363);
            // Set some JMS headers too
            // And also set a core props
            ((ActiveMQMessage) tm).getCoreMessage().putBytesProperty("bytes", new byte[] { 1, 2, 3 });
            // We add some JMSX ones too
            tm.setStringProperty("JMSXGroupID", "mygroup543");
            prod.send(tm);
            ids1.add(tm.getJMSMessageID());
        }
        JMSBridgeTest.log.trace("Sent the first messages");
        Session sessTarget = connTarget.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer cons = sessTarget.createConsumer(targetQueue);
        connTarget.start();
        List<TextMessage> msgs = new ArrayList<>();
        for (int i = 0; i < NUM_MESSAGES; i++) {
            TextMessage tm = (TextMessage) cons.receive(5000);
            Assert.assertNotNull(tm);
            Assert.assertEquals("message" + i, tm.getText());
            Assert.assertEquals("uhuh", tm.getStringProperty("wib"));
            Assert.assertTrue(tm.getBooleanProperty("cheese"));
            Assert.assertEquals(23, tm.getIntProperty("Sausages"));
            assertEquals((byte) 12, tm.getByteProperty("bacon"));
            assertEquals(17261762.12121d, tm.getDoubleProperty("toast"), 0.000000001);
            assertEquals(1212.1212f, tm.getFloatProperty("orange"), 0.000001);
            assertEquals(817217827L, tm.getLongProperty("blurg"));
            assertEquals((short) 26363, tm.getShortProperty("stst"));
            assertEqualsByteArrays(new byte[] { 1, 2, 3 }, ((ActiveMQMessage) tm).getCoreMessage().getBytesProperty("bytes"));
            Assert.assertEquals("mygroup543", tm.getStringProperty("JMSXGroupID"));
            if (on) {
                String header = tm.getStringProperty(ActiveMQJMSConstants.AMQ_MESSAGING_BRIDGE_MESSAGE_ID_LIST);
                Assert.assertNotNull(header);
                Assert.assertEquals(ids1.get(i), header);
                msgs.add(tm);
            }
        }
        if (on) {
            // Now we send them again back to the source
            Iterator<TextMessage> iter = msgs.iterator();
            List<String> ids2 = new ArrayList<>();
            while (iter.hasNext()) {
                Message msg = iter.next();
                prod.send(msg);
                ids2.add(msg.getJMSMessageID());
            }
            for (int i = 0; i < NUM_MESSAGES; i++) {
                TextMessage tm = (TextMessage) cons.receive(5000);
                Assert.assertNotNull(tm);
                Assert.assertEquals("message" + i, tm.getText());
                Assert.assertEquals("uhuh", tm.getStringProperty("wib"));
                Assert.assertTrue(tm.getBooleanProperty("cheese"));
                Assert.assertEquals(23, tm.getIntProperty("Sausages"));
                assertEquals((byte) 12, tm.getByteProperty("bacon"));
                assertEquals(17261762.12121d, tm.getDoubleProperty("toast"), 0.000001);
                assertEquals(1212.1212f, tm.getFloatProperty("orange"), 0.0000001);
                assertEquals(817217827L, tm.getLongProperty("blurg"));
                assertEquals((short) 26363, tm.getShortProperty("stst"));
                assertEqualsByteArrays(new byte[] { 1, 2, 3 }, ((ActiveMQMessage) tm).getCoreMessage().getBytesProperty("bytes"));
                Assert.assertEquals("mygroup543", tm.getStringProperty("JMSXGroupID"));
                String header = tm.getStringProperty(ActiveMQJMSConstants.AMQ_MESSAGING_BRIDGE_MESSAGE_ID_LIST);
                Assert.assertNotNull(header);
                Assert.assertEquals(ids1.get(i) + "," + ids2.get(i), header);
            }
        }
    } finally {
        if (bridge != null) {
            bridge.stop();
        }
        if (connSource != null) {
            connSource.close();
        }
        if (connTarget != null) {
            connTarget.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQMessage(org.apache.activemq.artemis.jms.client.ActiveMQMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) JMSBridgeImpl(org.apache.activemq.artemis.jms.bridge.impl.JMSBridgeImpl) Connection(javax.jms.Connection) ArrayList(java.util.ArrayList) ActiveMQMessage(org.apache.activemq.artemis.jms.client.ActiveMQMessage) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 4 with ActiveMQMessage

use of org.apache.activemq.artemis.jms.client.ActiveMQMessage in project activemq-artemis by apache.

the class MessageHeaderTest method testCopyOnJBossMessage.

@Test
public void testCopyOnJBossMessage() throws JMSException {
    ClientMessage clientMessage = new ClientMessageImpl(ActiveMQTextMessage.TYPE, true, 0, System.currentTimeMillis(), (byte) 4, 1000);
    ClientSession session = new FakeSession(clientMessage);
    ActiveMQMessage jbossMessage = ActiveMQMessage.createMessage(clientMessage, session);
    jbossMessage.clearProperties();
    MessageHeaderTestBase.configureMessage(jbossMessage);
    ActiveMQMessage copy = new ActiveMQMessage(jbossMessage, session);
    MessageHeaderTestBase.ensureEquivalent(jbossMessage, copy);
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientMessageImpl(org.apache.activemq.artemis.core.client.impl.ClientMessageImpl) ActiveMQMessage(org.apache.activemq.artemis.jms.client.ActiveMQMessage) Test(org.junit.Test)

Example 5 with ActiveMQMessage

use of org.apache.activemq.artemis.jms.client.ActiveMQMessage in project activemq-artemis by apache.

the class MessageHeaderTest method testForeignJMSDestination.

@Test
public void testForeignJMSDestination() throws JMSException {
    Message message = queueProducerSession.createMessage();
    Destination foreignDestination = new ForeignDestination();
    message.setJMSDestination(foreignDestination);
    ProxyAssertSupport.assertSame(foreignDestination, message.getJMSDestination());
    queueProducer.send(message);
    ProxyAssertSupport.assertSame(queue1, message.getJMSDestination());
    Message receivedMessage = queueConsumer.receive(2000);
    MessageHeaderTestBase.ensureEquivalent(receivedMessage, (ActiveMQMessage) message);
}
Also used : Destination(javax.jms.Destination) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) ActiveMQMessage(org.apache.activemq.artemis.jms.client.ActiveMQMessage) ActiveMQTextMessage(org.apache.activemq.artemis.jms.client.ActiveMQTextMessage) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) ActiveMQObjectMessage(org.apache.activemq.artemis.jms.client.ActiveMQObjectMessage) BytesMessage(javax.jms.BytesMessage) ActiveMQBytesMessage(org.apache.activemq.artemis.jms.client.ActiveMQBytesMessage) ActiveMQMapMessage(org.apache.activemq.artemis.jms.client.ActiveMQMapMessage) ActiveMQStreamMessage(org.apache.activemq.artemis.jms.client.ActiveMQStreamMessage) Test(org.junit.Test)

Aggregations

ActiveMQMessage (org.apache.activemq.artemis.jms.client.ActiveMQMessage)11 Message (javax.jms.Message)5 TextMessage (javax.jms.TextMessage)5 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)5 Test (org.junit.Test)4 BytesMessage (javax.jms.BytesMessage)3 Session (javax.jms.Session)3 Connection (javax.jms.Connection)2 MapMessage (javax.jms.MapMessage)2 MessageProducer (javax.jms.MessageProducer)2 ObjectMessage (javax.jms.ObjectMessage)2 StreamMessage (javax.jms.StreamMessage)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)2 ClientMessageImpl (org.apache.activemq.artemis.core.client.impl.ClientMessageImpl)2 ActiveMQBytesMessage (org.apache.activemq.artemis.jms.client.ActiveMQBytesMessage)2 ActiveMQMapMessage (org.apache.activemq.artemis.jms.client.ActiveMQMapMessage)2 ActiveMQObjectMessage (org.apache.activemq.artemis.jms.client.ActiveMQObjectMessage)2 ActiveMQStreamMessage (org.apache.activemq.artemis.jms.client.ActiveMQStreamMessage)2 ActiveMQTextMessage (org.apache.activemq.artemis.jms.client.ActiveMQTextMessage)2