Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. package com.spinn3r.artemis.activemq;
  2.  
  3. import org.apache.activemq.ActiveMQConnection;
  4. import org.apache.activemq.ActiveMQConnectionFactory;
  5. import org.apache.activemq.RedeliveryPolicy;
  6. import org.junit.Ignore;
  7. import org.junit.Test;
  8.  
  9. import javax.jms.*;
  10.  
  11. import java.util.Date;
  12.  
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertNull;
  15.  
  16. /**
  17.  *
  18.  */
  19. public class TestMessageRedelivery extends TestWithEmbeddedBroker2 {
  20.  
  21.     private static final int TIMEOUT = 60000;
  22.  
  23.     @Test
  24.     @Ignore
  25.     public void test1() throws Exception {
  26.  
  27.         int maximumRedeliveries = 5;
  28.  
  29.         String queueURL = "foo";
  30.  
  31.         ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( brokerURL );
  32.         ActiveMQConnection connection = (ActiveMQConnection)connectionFactory.createConnection();
  33.  
  34.         connection.start();
  35.  
  36.         Session session = connection.createSession( false, Session.CLIENT_ACKNOWLEDGE );
  37.  
  38.         Destination dest = session.createQueue( queueURL );
  39.  
  40.         MessageProducer producer = session.createProducer( dest );
  41.  
  42.         String text = String.format( "At the tone, the time is %s BEEP.", new Date() );
  43.         Message message = session.createTextMessage(text);
  44.  
  45.         System.out.printf( "Sending message ...\n" );
  46.         producer.send( message );
  47.  
  48.         producer.close();
  49.         connection.close();
  50.  
  51.         //*** now consume these messages.. .and never acknowledge them so they
  52.         // should keep getting resent
  53.  
  54.         System.out.printf( "Receiving message...\n" );
  55.  
  56.         for (int i = 0; i < maximumRedeliveries; i++) {
  57.  
  58.             message = consumeOneMessage( dest, TIMEOUT );
  59.  
  60.             assertNotNull( message );
  61.  
  62.             TextMessage textMessage = (TextMessage)message;
  63.  
  64.             System.out.printf( "Received message %s (%s)\n" , (i+1), textMessage.getText() );
  65.  
  66.         }
  67.  
  68.         System.out.printf( "Seeing if we get a final message...\n" );
  69.  
  70.         message = consumeOneMessage( dest, 5000 );
  71.  
  72.         assertNull( message );
  73.  
  74.     }
  75.  
  76.     private Message consumeOneMessage( Destination dest, int timeout ) throws JMSException {
  77.  
  78.         ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( brokerURL );
  79.  
  80.         ActiveMQConnection connection = (ActiveMQConnection)connectionFactory.createConnection();
  81.  
  82.         connection.start();
  83.         Session session = connection.createSession( false, Session.CLIENT_ACKNOWLEDGE );
  84.         MessageConsumer consumer = session.createConsumer( dest );
  85.  
  86.         Message message = consumer.receive( timeout );
  87.  
  88.         // TODO: we have to keep the connection open because activemq is smart enough
  89.         // to realize that the connection closed without an ack.
  90.  
  91.         //connection.stop();
  92.         //session.close();
  93.         //consumer.close();
  94.  
  95.         return message;
  96.  
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement