public void testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage()
      throws JMSException {
    MockControl cfControl = MockControl.createControl(TopicConnectionFactory.class);
    TopicConnectionFactory cf = (TopicConnectionFactory) cfControl.getMock();
    MockControl conControl = MockControl.createControl(TopicConnection.class);
    TopicConnection con = (TopicConnection) conControl.getMock();
    MockControl txSessionControl = MockControl.createControl(TopicSession.class);
    TopicSession txSession = (TopicSession) txSessionControl.getMock();
    MockControl nonTxSessionControl = MockControl.createControl(TopicSession.class);
    TopicSession nonTxSession = (TopicSession) nonTxSessionControl.getMock();

    cf.createTopicConnection();
    cfControl.setReturnValue(con, 1);
    con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
    conControl.setReturnValue(txSession, 1);
    txSession.getTransacted();
    txSessionControl.setReturnValue(true, 2);
    txSession.close();
    txSessionControl.setVoidCallable(1);
    con.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
    conControl.setReturnValue(nonTxSession, 1);
    nonTxSession.close();
    nonTxSessionControl.setVoidCallable(1);
    con.start();
    conControl.setVoidCallable(1);
    con.stop();
    conControl.setVoidCallable(1);
    con.close();
    conControl.setVoidCallable(1);

    cfControl.replay();
    conControl.replay();
    txSessionControl.replay();
    nonTxSessionControl.replay();

    CachingConnectionFactory scf = new CachingConnectionFactory(cf);
    scf.setReconnectOnException(false);
    Connection con1 = scf.createTopicConnection();
    Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session1.getTransacted();
    session1.close(); // should lead to rollback
    session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    session1.close(); // should be ignored
    con1.start();
    con1.close(); // should be ignored
    TopicConnection con2 = scf.createTopicConnection();
    Session session2 = con2.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
    session2.close(); // should be ignored
    session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session2.getTransacted();
    session2.close(); // should be ignored
    con2.start();
    con2.close(); // should be ignored
    scf.destroy(); // should trigger actual close

    cfControl.verify();
    conControl.verify();
    txSessionControl.verify();
    nonTxSessionControl.verify();
  }
  @Test
  public void testReceiveMessageTimeout() throws JMSException {
    JmsMessageReceiver receiver = new JmsMessageReceiver();
    receiver.setConnectionFactory(connectionFactory);

    receiver.setDestination(destination);

    reset(jmsTemplate, connectionFactory, destination, connection, session, messageConsumer);

    expect(connectionFactory.createConnection()).andReturn(connection).once();
    expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session).once();
    expect(session.getTransacted()).andReturn(false).once();
    expect(session.getAcknowledgeMode()).andReturn(Session.AUTO_ACKNOWLEDGE).once();

    expect(session.createConsumer(destination, null)).andReturn(messageConsumer).once();

    expect(messageConsumer.receive(5000L)).andReturn(null).once();

    connection.start();
    expectLastCall().once();

    replay(jmsTemplate, connectionFactory, destination, connection, session, messageConsumer);

    try {
      receiver.receive();
    } catch (ActionTimeoutException e) {
      Assert.assertTrue(
          e.getMessage().startsWith("Action timed out while receiving JMS message on"));
      return;
    }

    Assert.fail(
        "Missing " + CitrusRuntimeException.class + " because of receiveing message timeout");
  }
  @Test(timeout = 60000)
  public void testNotIdledWhenInUse() throws Exception {
    pooledFactory.setIdleTimeout(10);
    PooledConnection connection = (PooledConnection) pooledFactory.createConnection();
    Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    // let connection to get idle
    TimeUnit.MILLISECONDS.sleep(500);

    // get a connection from pool again, it should be the same underlying connection
    // as before and should not be idled out since an open session exists.
    PooledConnection connection2 = (PooledConnection) pooledFactory.createConnection();
    assertSame(connection.getConnection(), connection2.getConnection());

    // now the session is closed even when it should not be
    try {
      // any operation on session first checks whether session is closed
      s.getTransacted();
    } catch (javax.jms.IllegalStateException e) {
      assertTrue("Session should be fine, instead: " + e.getMessage(), false);
    }

    Connection original = connection.getConnection();

    connection.close();
    connection2.close();

    // let connection to get idle
    TimeUnit.MILLISECONDS.sleep(500);

    // get a connection from pool again, it should be a new Connection instance as the
    // old one should have been inactive and idled out.
    PooledConnection connection3 = (PooledConnection) pooledFactory.createConnection();
    assertNotSame(original, connection3.getConnection());
  }
  public void testRun() {

    // http://pookey.co.uk/wordpress/archives/74-playing-with-activemq-using-maven
    // http://java.sun.com/developer/technicalArticles/Ecommerce/jms/index.html

    String brokerURL = "tcp://localhost:61616";

    ConnectionFactory factory;
    Connection connection;
    Session session;
    MessageProducer producer;

    final String fedoraAppEmailQueue = FedoraAppConstants.JMS_ECTD_RESULTS_Q;

    String string_1 =
        "/home/chet/batch_space/codu/ectd/logs/codu.ectd.april-08-2011_17:15:36-297.txt"; // ingest
    // report
    String string_2 =
        "/home/chet/batch_space/codu/ectd/logs/codu.ectd.april-08-2011_17:15:36-297.csv"; // pid
    // report

    try {
      factory = new ActiveMQConnectionFactory(brokerURL);

      connection = factory.createConnection();
      connection.start();
      session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);

      System.out.println("\n\n** session transactions is set to :" + session.getTransacted());

      // send message

      Destination destination = session.createQueue(fedoraAppEmailQueue);
      producer = session.createProducer(destination);

      System.out.println("Creating Message ");
      Message message = session.createTextMessage(string_1 + "\n" + string_2 + "\n");
      producer.send(message);
      connection.close();

      // consume message
      connection = factory.createConnection();
      connection.start();
      session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
      destination = session.createQueue(fedoraAppEmailQueue);
      MessageConsumer consumer = session.createConsumer(destination);
      consumer.setMessageListener(null);
      consumer.setMessageListener(new ActiveMQListener());

      for (int i = 1; i < 10000000; i++) {
        System.out.println("looping");
      }
    } catch (Exception e) {
      System.out.println("Exception: " + e.getMessage());
    }
  } // testRun
Exemple #5
0
 public void onMessage(Message msg) {
   try {
     if (filter == null || filter.ok(msg)) {
       messageHandled.incrementAndGet();
     }
     if (session.getTransacted()) session.commit();
   } catch (Exception ex) {
     log.warn("Exception in on message: " + ex, ex);
     runEx = ex;
   }
   if (semaphore != null) semaphore.release();
 }
Exemple #6
0
 public void onMessage(Message msg) {
   try {
     if (filter != null) {
       if (filter.ok(msg)) messageHandled++;
     } else {
       messageHandled++;
     }
     if (session.getTransacted()) session.commit();
   } catch (Exception ex) {
     log.warn("Exception in on message: " + ex, ex);
     runEx = ex;
   }
 }
  @Test
  public void testWithMessageSelectorAndCustomTimeout() throws JMSException {
    JmsMessageReceiver receiver = new JmsMessageReceiver();
    receiver.setConnectionFactory(connectionFactory);

    receiver.setDestination(destination);
    receiver.setReceiveTimeout(10000L);

    Map<String, Object> controlHeaders = new HashMap<String, Object>();
    final Message<String> controlMessage =
        MessageBuilder.withPayload("<TestRequest><Message>Hello World!</Message></TestRequest>")
            .copyHeaders(controlHeaders)
            .build();

    Map<String, String> headers = new HashMap<String, String>();

    reset(jmsTemplate, connectionFactory, destination, connection, session, messageConsumer);

    expect(connectionFactory.createConnection()).andReturn(connection).once();
    expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session).once();
    expect(session.getTransacted()).andReturn(false).once();
    expect(session.getAcknowledgeMode()).andReturn(Session.AUTO_ACKNOWLEDGE).once();

    expect(session.createConsumer(destination, "Operation = 'sayHello'"))
        .andReturn(messageConsumer)
        .once();

    connection.start();
    expectLastCall().once();

    expect(messageConsumer.receive(10000L))
        .andReturn(
            new TextMessageImpl(
                "<TestRequest><Message>Hello World!</Message></TestRequest>", headers))
        .once();

    replay(jmsTemplate, connectionFactory, destination, connection, session, messageConsumer);

    Message<?> receivedMessage = receiver.receiveSelected("Operation = 'sayHello'");
    Assert.assertEquals(receivedMessage.getPayload(), controlMessage.getPayload());

    verify(jmsTemplate, connectionFactory, destination, connection, session, messageConsumer);
  }
  /** Test that close() hierarchically closes all child objects */
  public void testCloseHierarchy() throws Exception {
    Connection conn = cf.createConnection();
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = sess.createConsumer(topic1);
    MessageProducer producer = sess.createProducer(topic1);
    sess.createBrowser(queue1);
    Message m = sess.createMessage();

    conn.close();

    // Session

    /* If the session is closed then any method invocation apart from close()
     * will throw an IllegalStateException
     */
    try {
      sess.createMessage();
      fail("Session is not closed");
    } catch (javax.jms.IllegalStateException e) {
    }

    try {
      sess.getAcknowledgeMode();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      sess.getTransacted();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      sess.getMessageListener();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      sess.createProducer(queue1);
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      sess.createConsumer(queue1);
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    // Producer

    /* If the producer is closed then any method invocation apart from close()
     * will throw an IllegalStateException
     */
    try {
      producer.send(m);
      fail("Producer is not closed");
    } catch (javax.jms.IllegalStateException e) {
    }

    try {
      producer.getDisableMessageID();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      producer.getPriority();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      producer.getDestination();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      producer.getTimeToLive();
      fail("should throw IllegalStateException");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    // ClientConsumer

    try {
      consumer.getMessageSelector();
      fail("should throw exception");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      consumer.getMessageListener();
      fail("should throw exception");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    try {
      consumer.receive();
      fail("should throw exception");
    } catch (javax.jms.IllegalStateException e) {
      // OK
    }

    // Browser

  }