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();
  }
  public void testWithTopicConnection() throws JMSException {
    MockControl conControl = MockControl.createControl(TopicConnection.class);
    Connection con = (TopicConnection) conControl.getMock();

    con.start();
    conControl.setVoidCallable(1);
    con.stop();
    conControl.setVoidCallable(1);
    con.close();
    conControl.setVoidCallable(1);

    conControl.replay();

    SingleConnectionFactory scf = new SingleConnectionFactory(con);
    TopicConnection con1 = scf.createTopicConnection();
    con1.start();
    con1.stop(); // should be ignored
    con1.close(); // should be ignored
    TopicConnection con2 = scf.createTopicConnection();
    con2.start();
    con2.stop(); // should be ignored
    con2.close(); // should be ignored
    scf.destroy(); // should trigger actual close

    conControl.verify();
  }
  public void testConnectionFactory102WithTopic() throws JMSException {
    MockControl cfControl = MockControl.createControl(TopicConnectionFactory.class);
    TopicConnectionFactory cf = (TopicConnectionFactory) cfControl.getMock();
    MockControl conControl = MockControl.createControl(TopicConnection.class);
    TopicConnection con = (TopicConnection) conControl.getMock();

    cf.createTopicConnection();
    cfControl.setReturnValue(con, 1);
    con.start();
    conControl.setVoidCallable(1);
    con.stop();
    conControl.setVoidCallable(1);
    con.close();
    conControl.setVoidCallable(1);

    cfControl.replay();
    conControl.replay();

    SingleConnectionFactory scf = new SingleConnectionFactory102(cf, true);
    TopicConnection con1 = scf.createTopicConnection();
    con1.start();
    con1.close(); // should be ignored
    TopicConnection con2 = scf.createTopicConnection();
    con2.start();
    con2.close(); // should be ignored
    scf.destroy(); // should trigger actual close

    cfControl.verify();
    conControl.verify();
  }
예제 #4
0
  @Override
  public void reconnect() {
    int numTries = 0;
    int pauseInMs = 100;
    boolean connected = false;

    if (connectionFactory == null) {
      logger.warn("Asked to reconnect to AMQ but no connectionFactory was configured!");
      return;
    }

    synchronized (connectionMonitor) {
      close();
      while (!connected) {
        numTries++;
        try {
          connection = connectionFactory.createTopicConnection();
          connection.start();
          connected = true;
        } catch (JMSException ex) {
          logger.warn("Got error while trying to connect to activemq");
          try {
            Thread.sleep((long) pauseInMs);
          } catch (InterruptedException innerEx) {
            Thread.currentThread().interrupt();
            return;
          }
          if (numTries < 10) {
            pauseInMs += pauseInMs;
          }
        }
      }
    }
  }
예제 #5
0
  /**
   * 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);
      }
    }
  }
예제 #6
0
 public void start() {
   try {
     conn.start();
   } catch (JMSException e) {
     e.printStackTrace();
   }
 }
예제 #7
0
  public static TopicConnection getTopicConnection(Properties props)
      throws IOException, NamingException, JMSException {
    fixProviderUrl(props);
    String cnnFactoryName = props.getProperty(JNDI_TOPIC_CONECTION_NAME_PROPERTY);
    if (cnnFactoryName == null) {
      throw new IOException(
          "You must set the property "
              + DEFAULT_JNDI_CONECTION_NAME_PROPERTY
              + "in the JNDI property file");
    }
    Context ctx = new InitialContext(props);

    TopicConnectionFactory tcf = (TopicConnectionFactory) lookupObject(ctx, cnnFactoryName);
    TopicConnection cnn;
    String username = (String) props.get("username");

    if (username != null) {
      String password = (String) props.get("password");
      cnn = tcf.createTopicConnection(username, password);
    } else {
      cnn = tcf.createTopicConnection();
    }
    cnn.start();
    return cnn;
  }
예제 #8
0
 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);
   }
 }
  @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);
    }
  }
예제 #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();
 }
예제 #11
0
  private JMSProducer(final long timeout, String brokerURL) throws JMSException {

    // if brokerURL not defined before, get property
    if (brokerURL == null) {
      brokerURL = System.getProperty("broker.url");
    }

    // Create a ConnectionFactory
    topicConnectionFactory = new ActiveMQConnectionFactory(brokerURL);
    topicConnection = topicConnectionFactory.createTopicConnection();
    session = topicConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    topic = session.createTopic(TOPIC_NAME);

    topicConnection.start();

    producer = session.createProducer(topic);

    this.timeout = timeout;
    Runtime.getRuntime()
        .addShutdownHook(
            new Thread() {

              @Override
              public void run() {
                try {
                  topicConnection.stop();
                  producer.close();
                  session.close();
                  topicConnection.close();

                } catch (final JMSException e) {
                  System.err.println("Cannot stop the synchro service, probably already stopped?");
                }
              }
            });
  }
  /** 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();
  }
예제 #13
0
  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);
    }
  }