當前位置: 首頁>>代碼示例>>Java>>正文


Java Session.recover方法代碼示例

本文整理匯總了Java中javax.jms.Session.recover方法的典型用法代碼示例。如果您正苦於以下問題:Java Session.recover方法的具體用法?Java Session.recover怎麽用?Java Session.recover使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.jms.Session的用法示例。


在下文中一共展示了Session.recover方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testDlcWithBasicRecover

import javax.jms.Session; //導入方法依賴的package包/類
@Parameters({ "broker-hostname", "broker-port", "admin-username", "admin-password" })
@Test
public void testDlcWithBasicRecover(String brokerHostname,
                                         String port,
                                         String adminUsername,
                                         String adminPassword) throws Exception {
    String queueName = "testDlcWithBasicRecover";
    String dlcQueueName = "amq.dlq";
    InitialContext initialContextForQueue = ClientHelper
            .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
            .withQueue(queueName)
            .withQueue(dlcQueueName)
            .build();

    ConnectionFactory connectionFactory
            = (ConnectionFactory) initialContextForQueue.lookup(ClientHelper.CONNECTION_FACTORY);
    Connection connection = connectionFactory.createConnection();
    connection.start();

    // publish message
    Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = producerSession.createQueue(queueName);
    MessageProducer producer = producerSession.createProducer(queue);

    producer.send(producerSession.createTextMessage("Message for DLC test"));
    producerSession.close();

    // Consume published messages
    Session subscriberSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    Destination subscriberDestination = (Destination) initialContextForQueue.lookup(queueName);
    MessageConsumer consumer = subscriberSession.createConsumer(subscriberDestination);

    for (int iteration = 0; iteration < 6; iteration++) {
        Message message = consumer.receive(5000);
        Assert.assertNotNull(message, "Message was not received");
        subscriberSession.recover();
    }

    Connection dlcConsumerConnection = connectionFactory.createConnection();
    dlcConsumerConnection.start();
    Session dlcConsumerSession = dlcConsumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer dlcConsumer = dlcConsumerSession.createConsumer((Destination) initialContextForQueue.lookup(
            dlcQueueName));

    Message dlcMessage = dlcConsumer.receive(5000);
    Assert.assertNotNull(dlcMessage, "Dead lettered message was not received" + dlcMessage);
    String originQueue = dlcMessage.getStringProperty("x-origin-queue");
    Assert.assertEquals(originQueue, queueName, "Origin queue name did not match" + dlcMessage);
    String originExchange = dlcMessage.getStringProperty("x-origin-exchange");
    Assert.assertEquals(originExchange, "amq.direct", "Origin exchange name did not match" + dlcMessage);
    String originRoutingKey = dlcMessage.getStringProperty("x-origin-routing-key");
    Assert.assertEquals(originRoutingKey, queueName, "Origin routing key did not match" + dlcMessage);

    dlcConsumerConnection.close();
    connection.close();
}
 
開發者ID:wso2,項目名稱:message-broker,代碼行數:57,代碼來源:DeadLetterChannelTest.java

示例2: testConsumerWithBasicRecover

import javax.jms.Session; //導入方法依賴的package包/類
@Parameters({ "broker-port", "admin-username", "admin-password", "broker-hostname" })
@Test
public void testConsumerWithBasicRecover(String port,
                                        String adminUsername,
                                        String adminPassword,
                                        String brokerHostname) throws Exception {
    String queueName = "testConsumerWithBasicRecover";
    InitialContext initialContextForQueue = ClientHelper
            .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
            .withQueue(queueName)
            .build();

    ConnectionFactory connectionFactory
            = (ConnectionFactory) initialContextForQueue.lookup(ClientHelper.CONNECTION_FACTORY);
    Connection connection = connectionFactory.createConnection();
    connection.start();

    // publish message
    Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = producerSession.createQueue(queueName);
    MessageProducer producer = producerSession.createProducer(queue);

    TextMessage firstMessage = producerSession.createTextMessage("First message for reject test");
    TextMessage secondMessage = producerSession.createTextMessage("Second message for reject test");
    String correlationIdOne = "1";
    String correlationIdTwo = "2";
    firstMessage.setJMSCorrelationID(correlationIdOne);
    secondMessage.setJMSCorrelationID(correlationIdTwo);
    producer.send(firstMessage);
    producer.send(secondMessage);
    producerSession.close();

    // Consume published messages
    Session subscriberSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    Destination subscriberDestination = (Destination) initialContextForQueue.lookup(queueName);
    MessageConsumer consumer = subscriberSession.createConsumer(subscriberDestination);

    Message message = consumer.receive(5000);
    Assert.assertNotNull(message, "Message was not received");

    subscriberSession.recover();

    // Message order can change after recovering
    for (int i = 0; i < 2; i++) {
        message = consumer.receive(5000);
        Assert.assertNotNull(message, "Requeued message was not received");

        if (correlationIdOne.equals(message.getJMSCorrelationID())) {
            Assert.assertTrue(message.getJMSRedelivered(), "Redelivered flag was set in second message" + message);
        } else {
            Assert.assertFalse(message.getJMSRedelivered(), "Redelivered flag was set in first message");
        }

        message.acknowledge();
    }

    connection.close();
}
 
開發者ID:wso2,項目名稱:message-broker,代碼行數:59,代碼來源:QueueConsumerTest.java


注:本文中的javax.jms.Session.recover方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。