示例#1
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);
   }
 }
示例#2
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 == ");
    }
  }
  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);
        }
      }
    }
  }
示例#4
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();
 }
示例#5
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);
    }
  }