示例#1
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;
 }
示例#2
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);
   }
 }
示例#3
0
  private void writeMessage(String s) throws JMSException {

    TextMessage message = session.createTextMessage();
    message.setStringProperty("language", "java");
    message.setText(s);
    System.out.println("sending text message " + message);
    pub.publish(message);
  }
示例#4
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();
  }
示例#5
0
 public void stop() {
   try {
     session.close();
   } catch (JMSException e) {
     e.printStackTrace();
   }
   try {
     conn.close();
   } catch (JMSException e) {
     e.printStackTrace();
   }
 }
示例#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 == ");
    }
  }
示例#7
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);
    }
  }
示例#9
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);
    }
  }