Exemple #1
0
  @Test
  public void testRollbackTest() {
    JMSContext ctx = addContext(cf.createContext(JMSContext.SESSION_TRANSACTED));

    JMSProducer producer = ctx.createProducer();
    JMSConsumer cons = ctx.createConsumer(queue1);

    producer.send(queue1, context.createTextMessage("hello"));

    ctx.rollback();

    assertNull(cons.receiveNoWait());

    producer.send(queue1, context.createTextMessage("hello"));

    ctx.commit();

    assertNotNull(cons.receiveNoWait());

    ctx.commit();

    ctx.rollback();

    assertNull(cons.receiveNoWait());

    cons.close();
  }
Exemple #2
0
  @Test
  public void testContextStopAndCloseFromMessageListeners() throws Exception {
    final JMSContext context1 = context.createContext(Session.AUTO_ACKNOWLEDGE);
    JMSConsumer consumer1 = context1.createConsumer(queue1);

    final CountDownLatch latch1 = new CountDownLatch(1);

    InvalidMessageListener listener1 = new InvalidMessageListener(context1, latch1, 1);

    consumer1.setMessageListener(listener1);

    JMSProducer producer = context1.createProducer();
    Message msg = context1.createTextMessage("first message");
    producer.send(queue1, msg);

    latch1.await();

    Throwable error1 = listener1.getError();

    assertNotNull(error1);

    assertTrue(error1 instanceof IllegalStateRuntimeException);

    context1.close();

    final JMSContext context2 = context.createContext(Session.AUTO_ACKNOWLEDGE);
    JMSConsumer consumer2 = context2.createConsumer(queue1);

    final CountDownLatch latch2 = new CountDownLatch(1);

    InvalidMessageListener listener2 = new InvalidMessageListener(context2, latch2, 2);

    consumer2.setMessageListener(listener2);

    JMSProducer producer2 = context2.createProducer();
    Message msg2 = context2.createTextMessage("second message");
    producer2.send(queue1, msg2);

    latch2.await();

    Throwable error2 = listener2.getError();

    assertNotNull(error2);

    assertTrue(error2 instanceof IllegalStateRuntimeException);

    context2.close();
  }
Exemple #3
0
  @Test
  public void testDelay() throws Exception {
    JMSProducer producer = context.createProducer();

    JMSConsumer consumer = context.createConsumer(queue1);

    producer.setDeliveryDelay(500);

    long timeStart = System.currentTimeMillis();

    String strRandom = newXID().toString();

    producer.send(queue1, context.createTextMessage(strRandom));

    TextMessage msg = (TextMessage) consumer.receive(2500);

    assertNotNull(msg);

    long actualDelay = System.currentTimeMillis() - timeStart;
    assertTrue(
        "delay is not working, actualDelay=" + actualDelay,
        actualDelay >= 500 && actualDelay < 2000);

    assertEquals(strRandom, msg.getText());
  }
  @Override
  public int doStartTag() throws JspException {
    String context = "";
    try {
      System.out.println("jsp tag start...");

      ut.begin();
      JMSProducer producer = jmsContext.createProducer();
      TextMessage msg = jmsContext.createTextMessage("Hello JSP Tag");
      producer.send(queue, msg);
      context = jmsContext.toString();
      ut.commit();

      if (context.indexOf(transactionScope) == -1) {
        throw new JspException("NOT in transaction scope!");
      }
      // Get the writer object for output.
      JspWriter out = pageContext.getOut();

      // Perform substr operation on string.
      out.println(text);

    } catch (Exception e) {
      throw new JspException(e);
    }
    return SKIP_BODY;
  }
 @Override
 public void sendMessage(String text) {
   try {
     JMSProducer producer = jmsContext.createProducer();
     TextMessage message = jmsContext.createTextMessage(text);
     producer.send(testQueue, message);
   } catch (Exception e) {
     throw new EJBException(e);
   }
 }
Exemple #6
0
  @Test
  public void testExpire() throws Exception {
    JMSProducer producer = context.createProducer();

    producer.setTimeToLive(500);

    String strRandom = newXID().toString();

    producer.send(queue1, context.createTextMessage(strRandom));

    Thread.sleep(700);

    // Create consumer after message is expired, making it to expire at the server's
    JMSConsumer consumer = context.createConsumer(queue1);

    TextMessage msg = (TextMessage) consumer.receiveNoWait();

    // Time to live kicked in, so it's supposed to return null
    assertNull(msg);

    strRandom = newXID().toString();

    producer.send(queue1, context.createTextMessage(strRandom));

    Thread.sleep(700);

    // Receive second message, expiring on client
    msg = (TextMessage) consumer.receiveNoWait();

    assertNull(msg);

    strRandom = newXID().toString();

    producer.send(queue1, context.createTextMessage(strRandom));

    // will receive a message that's not expired now
    msg = (TextMessage) consumer.receiveNoWait();

    assertNotNull(msg);

    assertEquals(strRandom, msg.getText());
  }
Exemple #7
0
  @Test
  public void recoverAckTest() throws Exception {
    // Create JMSContext with CLIENT_ACKNOWLEDGE

    try (JMSContext context = cf.createContext(JMSContext.CLIENT_ACKNOWLEDGE)) {
      int numMessages = 10;

      TextMessage textMessage = null;

      // Create JMSConsumer from JMSContext
      JMSConsumer consumer = context.createConsumer(queue1);

      // Create JMSProducer from JMSContext
      JMSProducer producer = context.createProducer();

      // send messages
      for (int i = 0; i < numMessages; i++) {
        String message = "text message " + i;
        textMessage = context.createTextMessage(message);
        textMessage.setStringProperty("COM_SUN_JMS_TESTNAME", "recoverAckTest" + i);
        producer.send(queue1, textMessage);
      }

      // receive messages but do not acknowledge
      for (int i = 0; i < numMessages; i++) {
        textMessage = (TextMessage) consumer.receive(5000);
        assertNotNull(textMessage);
      }

      context.recover();

      // receive messages a second time followed by acknowledge
      for (int i = 0; i < numMessages; i++) {
        textMessage = (TextMessage) consumer.receive(5000);
        assertNotNull(textMessage);
      }

      // Acknowledge all messages
      context.acknowledge();
    }

    // doing this check with another context / consumer to make sure it was acked.
    try (JMSContext context = cf.createContext(JMSContext.CLIENT_ACKNOWLEDGE)) {
      // Create JMSConsumer from JMSContext
      JMSConsumer consumer = context.createConsumer(queue1);

      assertNull(consumer.receiveNoWait());
    }
  }
 protected final void sendMessages(
     JMSContext context, JMSProducer producer, Queue queue, final int total) {
   try {
     for (int j = 0; j < total; j++) {
       StringBuilder sb = new StringBuilder();
       for (int m = 0; m < 200; m++) {
         sb.append(random.nextLong());
       }
       Message msg = context.createTextMessage(sb.toString());
       msg.setIntProperty("counter", j);
       producer.send(queue, msg);
     }
   } catch (JMSException cause) {
     throw new JMSRuntimeException(cause.getMessage(), cause.getErrorCode(), cause);
   }
 }
Exemple #9
0
  @Test
  public void testReceiveText() throws Exception {
    JMSProducer producer = context.createProducer();

    JMSConsumer consumer = context.createConsumer(queue1);

    String randomStr = newXID().toString();

    System.out.println("RandomStr:" + randomStr);

    TextMessage sendMsg = context.createTextMessage(randomStr);
    producer.send(queue1, sendMsg);

    TextMessage receiveMsg = (TextMessage) consumer.receiveNoWait();

    assertEquals(randomStr, receiveMsg.getText());
  }
Exemple #10
0
  @Test
  public void testDeliveryMode() throws Exception {
    JMSProducer producer = context.createProducer();

    JMSConsumer consumer = context.createConsumer(queue1);

    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

    String strRandom = newXID().toString();

    producer.send(queue1, context.createTextMessage(strRandom));

    TextMessage msg = (TextMessage) consumer.receiveNoWait();

    assertNotNull(msg);

    assertEquals(DeliveryMode.NON_PERSISTENT, msg.getJMSDeliveryMode());
  }
Exemple #11
0
 @Test
 public void testCreateConsumerWithSelector() throws JMSException {
   final String filterName = "magicIndexMessage";
   final int total = 5;
   JMSProducer producer = context.createProducer();
   JMSConsumer consumerNoSelect = context.createConsumer(queue1);
   JMSConsumer consumer = context.createConsumer(queue1, filterName + "=TRUE");
   for (int i = 0; i < total; i++) {
     Message msg = context.createTextMessage("message " + i);
     msg.setBooleanProperty(filterName, i == 3);
     producer.send(queue1, msg);
   }
   Message msg0 = consumer.receive(500);
   Assert.assertNotNull(msg0);
   msg0.acknowledge();
   Assert.assertNull("no more messages", consumer.receiveNoWait());
   for (int i = 0; i < total - 1; i++) {
     Message msg = consumerNoSelect.receive(100);
     Assert.assertNotNull(msg);
     msg.acknowledge();
   }
   Assert.assertNull("no more messages", consumerNoSelect.receiveNoWait());
 }