Search in sources :

Example 21 with MessageId

use of org.apache.activemq.command.MessageId in project activemq-artemis by apache.

the class DurableSubscriptionOfflineTest method doTestOrderOnActivateDeactivate.

public void doTestOrderOnActivateDeactivate() throws Exception {
    final int messageCount = 1000;
    Connection con = null;
    Session session = null;
    final int numConsumers = 4;
    for (int i = 0; i <= numConsumers; i++) {
        con = createConnection("cli" + i);
        session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        session.createDurableSubscriber(topic, "SubsId", null, true);
        session.close();
        con.close();
    }
    final String url = "failover:(tcp://localhost:" + (broker.getTransportConnectors().get(1).getConnectUri()).getPort() + "?wireFormat.maxInactivityDuration=0)?" + "jms.watchTopicAdvisories=false&" + "jms.alwaysSyncSend=true&jms.dispatchAsync=true&" + "jms.sendAcksAsync=true&" + "initialReconnectDelay=100&maxReconnectDelay=30000&" + "useExponentialBackOff=true";
    final ActiveMQConnectionFactory clientFactory = new ActiveMQConnectionFactory(url);
    class CheckOrderClient implements Runnable {

        final int id;

        int runCount = 0;

        public CheckOrderClient(int id) {
            this.id = id;
        }

        @Override
        public void run() {
            try {
                synchronized (this) {
                    Connection con = clientFactory.createConnection();
                    con.setClientID("cli" + id);
                    con.start();
                    Session session = con.createSession(false, Session.CLIENT_ACKNOWLEDGE);
                    MessageConsumer consumer = session.createDurableSubscriber(topic, "SubsId", null, true);
                    int nextId = 0;
                    ++runCount;
                    int i = 0;
                    for (; i < messageCount / 2; i++) {
                        Message message = consumer.receiveNoWait();
                        if (message == null) {
                            break;
                        }
                        long producerSequenceId = new MessageId(message.getJMSMessageID()).getProducerSequenceId();
                        assertEquals(id + " expected order: runCount: " + runCount + " id: " + message.getJMSMessageID(), ++nextId, producerSequenceId);
                    }
                    LOG.info(con.getClientID() + " peeked " + i);
                    session.close();
                    con.close();
                }
            } catch (Throwable e) {
                e.printStackTrace();
                exceptions.add(e);
            }
        }
    }
    Runnable producer = new Runnable() {

        final String payLoad = new String(new byte[600]);

        @Override
        public void run() {
            try {
                Connection con = createConnection();
                final Session sendSession = con.createSession(true, Session.SESSION_TRANSACTED);
                MessageProducer producer = sendSession.createProducer(topic);
                for (int i = 0; i < messageCount; i++) {
                    producer.send(sendSession.createTextMessage(payLoad));
                }
                LOG.info("About to commit: " + messageCount);
                sendSession.commit();
                LOG.info("committed: " + messageCount);
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
                exceptions.add(e);
            }
        }
    };
    ExecutorService executorService = Executors.newCachedThreadPool();
    // concurrent commit and activate
    for (int i = 0; i < numConsumers; i++) {
        final CheckOrderClient client = new CheckOrderClient(i);
        for (int j = 0; j < 100; j++) {
            executorService.execute(client);
        }
    }
    executorService.execute(producer);
    executorService.shutdown();
    executorService.awaitTermination(5, TimeUnit.MINUTES);
    con.close();
    assertTrue("no exceptions: " + exceptions, exceptions.isEmpty());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ExecutorService(java.util.concurrent.ExecutorService) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) MessageId(org.apache.activemq.command.MessageId)

Example 22 with MessageId

use of org.apache.activemq.command.MessageId in project activemq-artemis by apache.

the class RecoveryBrokerTest method XtestWildCardSubscriptionPreservedOnRestart.

/**
 * Used to verify that after a broker restart durable subscriptions that use
 * wild cards are still wild card subscription after broker restart.
 *
 * @throws Exception
 */
// need to revist!!!
public void XtestWildCardSubscriptionPreservedOnRestart() throws Exception {
    ActiveMQDestination dest1 = new ActiveMQTopic("TEST.A");
    ActiveMQDestination dest2 = new ActiveMQTopic("TEST.B");
    ActiveMQDestination dest3 = new ActiveMQTopic("TEST.C");
    ActiveMQDestination wildDest = new ActiveMQTopic("TEST.>");
    ArrayList<MessageId> sentBeforeRestart = new ArrayList<>();
    ArrayList<MessageId> sentBeforeCreateConsumer = new ArrayList<>();
    ArrayList<MessageId> sentAfterCreateConsumer = new ArrayList<>();
    // Setup a first connection
    {
        StubConnection connection1 = createConnection();
        ConnectionInfo connectionInfo1 = createConnectionInfo();
        connectionInfo1.setClientId("A");
        SessionInfo sessionInfo1 = createSessionInfo(connectionInfo1);
        ProducerInfo producerInfo1 = createProducerInfo(sessionInfo1);
        connection1.send(connectionInfo1);
        connection1.send(sessionInfo1);
        connection1.send(producerInfo1);
        // Create the durable subscription.
        ConsumerInfo consumerInfo1 = createConsumerInfo(sessionInfo1, wildDest);
        consumerInfo1.setSubscriptionName("test");
        consumerInfo1.setPrefetchSize(100);
        connection1.send(consumerInfo1);
        // Close the subscription.
        connection1.send(closeConsumerInfo(consumerInfo1));
        // Send the messages
        for (int i = 0; i < 4; i++) {
            Message m = createMessage(producerInfo1, dest1, DeliveryMode.PERSISTENT);
            connection1.send(m);
            sentBeforeRestart.add(m.getMessageId());
        }
        connection1.request(closeConnectionInfo(connectionInfo1));
        connection1.stop();
    }
    // Restart the broker.
    restartBroker();
    // Get a connection to the new broker.
    {
        StubConnection connection2 = createConnection();
        ConnectionInfo connectionInfo2 = createConnectionInfo();
        connectionInfo2.setClientId("A");
        SessionInfo sessionInfo2 = createSessionInfo(connectionInfo2);
        connection2.send(connectionInfo2);
        connection2.send(sessionInfo2);
        ProducerInfo producerInfo2 = createProducerInfo(sessionInfo2);
        connection2.send(producerInfo2);
        // Send messages before the durable subscription is re-activated.
        for (int i = 0; i < 4; i++) {
            Message m = createMessage(producerInfo2, dest2, DeliveryMode.PERSISTENT);
            connection2.send(m);
            sentBeforeCreateConsumer.add(m.getMessageId());
        }
        // Re-open the subscription.
        ConsumerInfo consumerInfo2 = createConsumerInfo(sessionInfo2, wildDest);
        consumerInfo2.setSubscriptionName("test");
        consumerInfo2.setPrefetchSize(100);
        connection2.send(consumerInfo2);
        // Send messages after the subscription is activated.
        for (int i = 0; i < 4; i++) {
            Message m = createMessage(producerInfo2, dest3, DeliveryMode.PERSISTENT);
            connection2.send(m);
            sentAfterCreateConsumer.add(m.getMessageId());
        }
        // We should get the recovered messages...
        for (int i = 0; i < 4; i++) {
            Message m2 = receiveMessage(connection2);
            assertNotNull("Recovered message missing: " + i, m2);
            assertEquals(sentBeforeRestart.get(i), m2.getMessageId());
        }
        // reactivated.
        for (int i = 0; i < 4; i++) {
            Message m2 = receiveMessage(connection2);
            assertNotNull("Before activated message missing: " + i, m2);
            assertEquals(sentBeforeCreateConsumer.get(i), m2.getMessageId());
        }
        // reactivated.
        for (int i = 0; i < 4; i++) {
            Message m2 = receiveMessage(connection2);
            assertNotNull("After activated message missing: " + i, m2);
            assertEquals("" + i, sentAfterCreateConsumer.get(i), m2.getMessageId());
        }
        assertNoMessagesLeft(connection2);
    }
}
Also used : ActiveMQTopic(org.apache.activemq.command.ActiveMQTopic) ProducerInfo(org.apache.activemq.command.ProducerInfo) ConsumerInfo(org.apache.activemq.command.ConsumerInfo) Message(org.apache.activemq.command.Message) ArrayList(java.util.ArrayList) SessionInfo(org.apache.activemq.command.SessionInfo) ConnectionInfo(org.apache.activemq.command.ConnectionInfo) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) MessageId(org.apache.activemq.command.MessageId)

Example 23 with MessageId

use of org.apache.activemq.command.MessageId in project activemq-artemis by apache.

the class OpenWireProtocolManager method fireAdvisory.

/*
    * See AdvisoryBroker.fireAdvisory()
    */
public void fireAdvisory(AMQConnectionContext context, ActiveMQTopic topic, Command command, ConsumerId targetConsumerId, String originalConnectionId) throws Exception {
    if (!this.isSupportAdvisory()) {
        return;
    }
    ActiveMQMessage advisoryMessage = new ActiveMQMessage();
    if (originalConnectionId == null) {
        originalConnectionId = context.getConnectionId().getValue();
    }
    advisoryMessage.setStringProperty(MessageUtil.CONNECTION_ID_PROPERTY_NAME.toString(), originalConnectionId);
    advisoryMessage.setStringProperty(AdvisorySupport.MSG_PROPERTY_ORIGIN_BROKER_NAME, getBrokerName());
    String id = getBrokerId() != null ? getBrokerId().getValue() : "NOT_SET";
    advisoryMessage.setStringProperty(AdvisorySupport.MSG_PROPERTY_ORIGIN_BROKER_ID, id);
    String url = context.getConnection().getLocalAddress();
    advisoryMessage.setStringProperty(AdvisorySupport.MSG_PROPERTY_ORIGIN_BROKER_URL, url);
    // set the data structure
    advisoryMessage.setDataStructure(command);
    advisoryMessage.setPersistent(false);
    advisoryMessage.setType(AdvisorySupport.ADIVSORY_MESSAGE_TYPE);
    advisoryMessage.setMessageId(new MessageId(advisoryProducerId, messageIdGenerator.getNextSequenceId()));
    advisoryMessage.setTargetConsumerId(targetConsumerId);
    advisoryMessage.setDestination(topic);
    advisoryMessage.setResponseRequired(false);
    advisoryMessage.setProducerId(advisoryProducerId);
    boolean originalFlowControl = context.isProducerFlowControl();
    final AMQProducerBrokerExchange producerExchange = new AMQProducerBrokerExchange();
    producerExchange.setConnectionContext(context);
    producerExchange.setProducerState(new ProducerState(new ProducerInfo()));
    try {
        context.setProducerFlowControl(false);
        AMQSession sess = context.getConnection().getAdvisorySession();
        if (sess != null) {
            sess.send(producerExchange.getProducerState().getInfo(), advisoryMessage, false);
        }
    } finally {
        context.setProducerFlowControl(originalFlowControl);
    }
}
Also used : AMQSession(org.apache.activemq.artemis.core.protocol.openwire.amq.AMQSession) ProducerInfo(org.apache.activemq.command.ProducerInfo) ProducerState(org.apache.activemq.state.ProducerState) AMQProducerBrokerExchange(org.apache.activemq.artemis.core.protocol.openwire.amq.AMQProducerBrokerExchange) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) MessageId(org.apache.activemq.command.MessageId)

Example 24 with MessageId

use of org.apache.activemq.command.MessageId in project activemq-artemis by apache.

the class AMQConsumer method acknowledge.

/**
 * The acknowledgement in openwire is done based on intervals.
 * We will iterate through the list of delivering messages at {@link ServerConsumer#getDeliveringReferencesBasedOnProtocol(boolean, Object, Object)}
 * and add those to the Transaction.
 * Notice that we will start a new transaction on the cases where there is no transaction.
 */
public void acknowledge(MessageAck ack) throws Exception {
    MessageId first = ack.getFirstMessageId();
    MessageId last = ack.getLastMessageId();
    if (first == null) {
        first = last;
    }
    // if it's browse only, nothing to be acked, we just remove the lists
    boolean removeReferences = !serverConsumer.isBrowseOnly();
    if (ack.isRedeliveredAck() || ack.isDeliveredAck() || ack.isExpiredAck()) {
        removeReferences = false;
    }
    List<MessageReference> ackList = serverConsumer.getDeliveringReferencesBasedOnProtocol(removeReferences, first, last);
    acquireCredit(ack.getMessageCount());
    if (removeReferences) {
        Transaction originalTX = session.getCoreSession().getCurrentTransaction();
        Transaction transaction;
        if (originalTX == null) {
            transaction = session.getCoreSession().newTransaction();
        } else {
            transaction = originalTX;
        }
        if (ack.isIndividualAck() || ack.isStandardAck()) {
            for (MessageReference ref : ackList) {
                ref.acknowledge(transaction);
            }
        } else if (ack.isPoisonAck()) {
            for (MessageReference ref : ackList) {
                Throwable poisonCause = ack.getPoisonCause();
                if (poisonCause != null) {
                    ref.getMessage().putStringProperty(OpenWireMessageConverter.AMQ_MSG_DLQ_DELIVERY_FAILURE_CAUSE_PROPERTY, new SimpleString(poisonCause.toString()));
                }
                ref.getQueue().sendToDeadLetterAddress(transaction, ref);
            }
        }
        if (originalTX == null) {
            transaction.commit(true);
        }
    }
    if (ack.isExpiredAck()) {
        for (MessageReference ref : ackList) {
            ref.getQueue().expire(ref);
        }
    }
}
Also used : Transaction(org.apache.activemq.artemis.core.transaction.Transaction) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) MessageId(org.apache.activemq.command.MessageId)

Example 25 with MessageId

use of org.apache.activemq.command.MessageId in project activemq-artemis by apache.

the class MessageIdTest method createObject.

@Override
public Object createObject() throws Exception {
    MessageId info = new MessageId();
    populateObject(info);
    return info;
}
Also used : MessageId(org.apache.activemq.command.MessageId)

Aggregations

MessageId (org.apache.activemq.command.MessageId)38 ActiveMQTextMessage (org.apache.activemq.command.ActiveMQTextMessage)10 ActiveMQMessage (org.apache.activemq.command.ActiveMQMessage)5 MessageNotWriteableException (javax.jms.MessageNotWriteableException)4 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)4 ActiveMQDestination (org.apache.activemq.command.ActiveMQDestination)4 ProducerId (org.apache.activemq.command.ProducerId)4 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 Message (org.apache.activemq.command.Message)3 ProducerInfo (org.apache.activemq.command.ProducerInfo)3 Connection (javax.jms.Connection)2 JMSException (javax.jms.JMSException)2 Message (javax.jms.Message)2 MessageConsumer (javax.jms.MessageConsumer)2 MessageProducer (javax.jms.MessageProducer)2 Session (javax.jms.Session)2 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)2 MessageReference (org.apache.activemq.broker.region.MessageReference)2 ConsumerInfo (org.apache.activemq.command.ConsumerInfo)2