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();
  }
  /**
   * Tries to reconnect to the durable topic. This method blocks until the try is successful or this
   * provider is stopped.
   */
  private void reconnect() {
    for (; ; ) {

      if (stopped) {
        return;
      }
      try {
        // close subscriber if not previously closed
        if (subscriber != null) {
          subscriber.close();
        }
        if (connection != null) {
          connection.close();
        }
        connection = connectionFactory.createTopicConnection();
        if (clientID != null) {
          connection.setClientID(clientID);
        }
        TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = topicFactory.createTopic(topicName);
        subscriber = session.createDurableSubscriber(topic, name);
        connection.start();
        return;
      } catch (JMSException e) {
        logger.error("could not connect to durable topic, topic: " + topicName, e);
        // step back for a while
        backOffAfterJMSException(e);
      }
    }
  }
Beispiel #3
0
 public static TopicSubscriber getTopicSubscriber(TopicConnection cnn, String topicName)
     throws JMSException {
   TopicSession session = cnn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
   Topic topic = session.createTopic(topicName);
   TopicSubscriber receiver = session.createSubscriber(topic);
   return receiver;
 }
 public void onMessage(Message message) {
   try {
     MapMessage request = (MapMessage) message;
     String correlationID = request.getJMSCorrelationID();
     int itemId = request.getInt("itemId");
     // Retrieve the connection factory
     connectionFactory =
         (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName);
     // get the bids history
     String html = getBidHistory(itemId);
     // send the reply
     TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo();
     if (temporaryTopic != null) {
       // create a connection
       connection = connectionFactory.createTopicConnection();
       // create a session: no transaction, auto ack
       session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
       TextMessage reply = session.createTextMessage();
       reply.setJMSCorrelationID(correlationID);
       reply.setText(html);
       replier = session.createPublisher(null); // unidentified publisher
       connection.start();
       replier.publish(temporaryTopic, reply);
       replier.close();
       session.close();
       connection.stop();
       connection.close();
     }
   } catch (Exception e) {
     throw new EJBException("Message traitment failed for MDB_ViewBidHistory: " + e);
   }
 }
Beispiel #5
0
  TopicSession createTopicSession() {
    TopicSession result = null;

    while (result == null) {
      try {
        result = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
      } catch (JMSException ex) {
        reconnect();
      }
    }
    return result;
  }
Beispiel #6
0
  public ChatPublisher(String name, String topicName, boolean isDurable) throws JMSException {
    this.name = name;
    TopicConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
    conn = factory.createTopicConnection();
    session = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    pub = session.createPublisher(new ActiveMQTopic(topicName));

    if (isDurable) {
      pub.setDeliveryMode(DeliveryMode.PERSISTENT);
      //            pub.setTimeToLive(20000);
      System.out.println(" === persistence == ");
    }
  }
  @Override
  public void setUp() throws Exception {
    super.setUp();
    try {
      queueConnection = queueConnectionFactory.createQueueConnection();
      queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      topicConnection = topicConnectionFactory.createTopicConnection();
      topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

      queueConnection.start();
      topicConnection.start();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  protected void produceJMSMessage(SerializableEventBundle message)
      throws JMSBusNotActiveException {
    InitialContext ctx;
    Topic nuxeoTopic;
    try {
      ctx = new InitialContext();
      nuxeoTopic = (Topic) ctx.lookup(NUXEO_JMS_TOPIC);
    } catch (NamingException e) {
      jmsBusIsActive = false;
      throw new JMSBusNotActiveException(e);
    }

    TopicConnection nuxeoTopicConnection = null;
    TopicSession nuxeoTopicSession = null;
    TopicPublisher nuxeoMessagePublisher = null;
    try {
      TopicConnectionFactory factory =
          (TopicConnectionFactory) ctx.lookup("TopicConnectionFactory");
      nuxeoTopicConnection = factory.createTopicConnection();
      nuxeoTopicSession =
          nuxeoTopicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);

      ObjectMessage jmsMessage = nuxeoTopicSession.createObjectMessage(message);

      // add Headers for JMS message
      jmsMessage.setStringProperty("BundleEvent", message.getEventBundleName());

      nuxeoMessagePublisher = nuxeoTopicSession.createPublisher(nuxeoTopic);

      nuxeoMessagePublisher.send(jmsMessage);
      log.debug("Event bundle " + message.getEventBundleName() + " forwarded to JMS topic");

    } catch (Exception e) {
      log.error("Error during JMS forwarding", e);
    } finally {
      if (nuxeoTopicSession != null) {
        try {
          if (nuxeoMessagePublisher != null) {
            nuxeoMessagePublisher.close();
          }
          nuxeoTopicConnection.close();
          nuxeoTopicSession.close();
        } catch (JMSException e) {
          log.error("Error during JMS cleanup", e);
        }
      }
    }
  }
Beispiel #9
0
  public static void drainTopic(TopicConnection cnn, String topic) throws Exception {
    TopicSession session = cnn.createTopicSession(false, Session.DUPS_OK_ACKNOWLEDGE);
    Topic t = session.createTopic(topic);

    TopicSubscriber subscriber = session.createSubscriber(t);
    Message msg = subscriber.receiveNoWait();
    while (msg != null) {
      try {
        msg.acknowledge();
      } catch (JMSException e) {

      }
      msg = subscriber.receiveNoWait();
    }
    subscriber.close();
    session.close();
  }
Beispiel #10
0
 public void start() throws JMSException, NamingException {
   Context ctx = getInitialContext();
   TopicConnectionFactory tcf = (TopicConnectionFactory) ctx.lookup("ConnectionFactory");
   Topic t = (Topic) ctx.lookup("topic/i_jms");
   TopicConnection tconn = tcf.createTopicConnection();
   TopicSession tses = tconn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
   TopicPublisher tpub = tses.createPublisher(t);
   tconn.start();
   for (int i = 0; i < 10; i++) {
     Producer.sendMessage("Message= " + i, tses, tpub);
     try {
       Thread.sleep(3000);
     } catch (InterruptedException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
   }
   tpub.close();
 }
Beispiel #11
0
 public static void topicPublish(
     TopicConnection cnn,
     String topicName,
     String payload,
     boolean transacted,
     int ack,
     String replyTo)
     throws JMSException {
   TopicSession session = cnn.createTopicSession(transacted, ack);
   Topic topic = session.createTopic(topicName);
   TopicPublisher publisher = session.createPublisher(topic);
   TextMessage msg = session.createTextMessage();
   msg.setText(payload);
   msg.setJMSDeliveryMode(ack);
   if (replyTo != null) {
     msg.setJMSReplyTo(session.createTopic(replyTo));
   }
   publisher.publish(msg);
   publisher.close();
   session.close();
 }
  public tibjmsLoadBalancedTopicPublisher(String[] args) {

    parseArgs(args);

    /* print parameters */
    System.out.println(
        "\n------------------------------------------------------------------------");
    System.out.println("tibjmsLoadBalancedTopicPublisher SAMPLE");
    System.out.println("------------------------------------------------------------------------");
    System.out.println("Servers...................... " + serverList);
    System.out.println("User......................... " + (userName != null ? userName : "******"));
    System.out.println("Topic........................ " + topicName);
    System.out.println("Messages..................... " + messages);
    System.out.println("Delay........................ " + delay);
    System.out.println(
        "------------------------------------------------------------------------\n");

    System.err.println("Publishing on topic '" + topicName + "'\n");

    try {
      HashMap properties = new HashMap();
      Integer metric;
      if (balanceByConnections)
        metric = new Integer(Tibjms.FACTORY_LOAD_BALANCE_METRIC_CONNECTIONS);
      else metric = new Integer(Tibjms.FACTORY_LOAD_BALANCE_METRIC_BYTE_RATE);

      properties.put(Tibjms.FACTORY_LOAD_BALANCE_METRIC, metric);

      TopicConnectionFactory factory =
          new com.tibco.tibjms.TibjmsTopicConnectionFactory(serverList, null, properties);

      TopicConnection connection = factory.createTopicConnection(userName, password);

      TopicSession session =
          connection.createTopicSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);

      /*
       * Use createTopic() to enable publishing into dynamic topics.
       */
      javax.jms.Topic topic = session.createTopic(topicName);

      TopicPublisher publisher = session.createPublisher(topic);

      /* publish messages */
      for (int i = 0; i < messages; i++) {
        javax.jms.TextMessage message = session.createTextMessage();
        String text = "Load balanced message " + i;
        message.setText(text);
        publisher.publish(message);
        System.err.println(
            "Published message " + i + " to server " + Tibjms.getConnectionActiveURL(connection));
        try {
          Thread.sleep(delay * 1000);
        } catch (InterruptedException e) {
        }
      }

      connection.close();
    } catch (JMSException e) {
      e.printStackTrace();
      System.exit(0);
    }
  }
  /** See TCK test: topicconntests.connNotStartedTopicTest */
  public void testCannotReceiveMessageOnStoppedConnection() throws Exception {
    TopicConnection conn1 = ((TopicConnectionFactory) cf).createTopicConnection();
    TopicConnection conn2 = ((TopicConnectionFactory) cf).createTopicConnection();

    TopicSession sess1 = conn1.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    TopicSession sess2 = conn2.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

    TopicSubscriber sub1 = sess1.createSubscriber(topic1);
    TopicSubscriber sub2 = sess2.createSubscriber(topic1);

    conn1.start();

    Connection conn3 = cf.createConnection();

    Session sess3 = conn3.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer prod = sess3.createProducer(topic1);
    prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

    final int NUM_MESSAGES = 10;

    for (int i = 0; i < NUM_MESSAGES; i++) {
      TextMessage tm = sess3.createTextMessage("hello");
      prod.send(tm);
    }

    int count = 0;
    while (true) {
      TextMessage tm = (TextMessage) sub1.receive(500);
      if (tm == null) {
        break;
      }
      assertEquals("hello", tm.getText());
      count++;
    }
    assertEquals(NUM_MESSAGES, count);

    Message m = sub2.receive(200);

    assertNull(m);

    conn2.start();

    count = 0;
    while (true) {
      TextMessage tm = (TextMessage) sub2.receive(500);
      if (tm == null) {
        break;
      }
      assertEquals("hello", tm.getText());
      count++;
    }
    assertEquals(NUM_MESSAGES, count);

    log.debug("all messages received by sub2");

    conn1.close();

    conn2.close();

    conn3.close();
  }
  public tibjmsTopicSubscriber(String[] args) {

    parseArgs(args);

    /* print parameters */
    System.err.println(
        "\n------------------------------------------------------------------------");
    System.err.println("tibjmsTopicSubscriber SAMPLE");
    System.err.println("------------------------------------------------------------------------");
    System.err.println(
        "Server....................... " + ((serverUrl != null) ? serverUrl : "localhost"));
    System.err.println(
        "User......................... " + ((userName != null) ? userName : "******"));
    System.err.println("Topic........................ " + topicName);
    System.err.println(
        "------------------------------------------------------------------------\n");

    try {
      tibjmsUtilities.initSSLParams(serverUrl, args);
    } catch (JMSSecurityException e) {
      System.err.println(
          "JMSSecurityException: " + e.getMessage() + ", provider=" + e.getErrorCode());
      e.printStackTrace();
      System.exit(0);
    }

    if (topicName == null) {
      System.err.println("Error: must specify topic name");
      usage();
    }

    System.err.println("Subscribing to topic: " + topicName);

    try {
      TopicConnectionFactory factory = new com.tibco.tibjms.TibjmsTopicConnectionFactory(serverUrl);

      TopicConnection connection = factory.createTopicConnection(userName, password);

      TopicSession session =
          connection.createTopicSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);

      /*
       * Use createTopic() to enable subscriptions to dynamic topics.
       */
      javax.jms.Topic topic = session.createTopic(topicName);

      TopicSubscriber subscriber = session.createSubscriber(topic);

      connection.start();

      /* read topic messages */
      while (true) {
        javax.jms.Message message = subscriber.receive();
        if (message == null) break;

        System.err.println("Received message: " + message);
      }

      connection.close();
    } catch (JMSException e) {
      System.err.println("JMSException: " + e.getMessage() + ", provider=" + e.getErrorCode());
      e.printStackTrace();
      System.exit(0);
    }
  }