public void doSend() {

    QueueSession queueSession = null;
    QueueSender queueSender = null;
    TextMessage message = null;

    if (doSetup()) {

      try {
        queueConnection = queueConnectionFactory.createQueueConnection();
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueSender = queueSession.createSender(queue);
        message = queueSession.createTextMessage();
        for (int i = 0; i < NUM_MSGS; i++) {
          message.setText("This is message " + (i + 1));
          System.out.println("Sending message: " + message.getText());
          queueSender.send(message);
        }

        /*
         * Send a non-text control message indicating end of messages.
         */
        queueSender.send(queueSession.createMessage());
      } catch (JMSException e) {
        log.error("JMS Send Exception occurred: " + e.toString());
      } finally {
        doCleanup();
      }
    }
  }
 public int read(byte[] buffer) throws IOException {
   try {
     return _message.readBytes(buffer);
   } catch (JMSException e) {
     throw new IOException(e.toString());
   }
 }
예제 #3
0
  public void createJMSObject() throws Config4JMSException {
    Queue queue;
    Session session;

    session = creator.getSession();
    try {
      // --------
      // One of the following:
      //    createBrowser(queue)
      //    createBrowser(queue, messageSelector)
      // --------
      queue = config4jms.getQueue(createQueue);
      if (createMessageSelector == null) {
        queueBrowser = (QueueBrowser) session.createBrowser(queue);
      } else {
        queueBrowser = (QueueBrowser) session.createBrowser(queue, createMessageSelector);
      }
    } catch (JMSException ex) {
      throw new Config4JMSException(
          ex,
          cfg.fileName()
              + ": error in "
              + "creating object for "
              + scope
              + "; Session.createConsumer() failed: "
              + ex.toString());
    }
  }
  /** use auto acknowledge which automatically has the client say OK */
  public void doReceiveAuto() {

    if (doSetup()) {
      try {
        queueConnection = queueConnectionFactory.createQueueConnection();
        QueueSession queueSession =
            queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        queueConnection.start();
        while (true) {
          Message m = queueReceiver.receive(1);
          if (m != null) {
            if (m instanceof TextMessage) {
              TextMessage message = (TextMessage) m;
              log.debug("Reading message:==> " + message.getText());
            } else {
              break;
            }
          }
        }
      } catch (JMSException e) {
        log.error("Listen Exception occurred: " + e.toString());
      } finally {
        doCleanup();
      }
    }
  }
 public int read(byte[] buffer, int offset, int length) throws IOException {
   try {
     if (offset == 0) return _message.readBytes(buffer, length);
     else return super.read(buffer, offset, length);
   } catch (JMSException e) {
     throw new IOException(e.toString());
   }
 }
예제 #6
0
 Object getReport(Message m) {
   try {
     return ((TextMessage) m).getText();
   } catch (JMSException e) {
     e.printStackTrace(System.out);
     return e.toString();
   }
 }
 @Override
 public void onMessage(Message message) {
   TextMessage msgText = (TextMessage) message;
   try {
     System.out.format("Consumed message -> %s", msgText.getText());
   } catch (JMSException e) {
     System.out.format("%s", e.toString());
   }
 }
 /** Si disconnette dalla destinazione JMS. */
 protected void disconnect() {
   if (connection != null) {
     try {
       connection.close();
       connection = null;
     } catch (JMSException e) {
       System.out.println("Disconnection problem: " + e.toString());
     }
   }
 }
예제 #9
0
 // The main thread receives the messages, put them in the queue
 @Override
 public void onMessage(Message message) {
   lastMsgTime = new Date().getTime();
   if (message instanceof TextMessage) {
     try {
       onMessage(((TextMessage) message).getText());
     } catch (JMSException e) {
       log.info("ERROR: viz failed to extract text from TextMessage: " + e.toString());
     }
   }
 }
 /** Interrompe la connessione con la destinazione JMS */
 protected void stopReceiveMessages() {
   if (this.receiving) {
     try {
       connection.stop();
       this.receiving = false;
     } catch (JMSException e) {
       System.out.println("JMSChatSubscriber: " + e.toString());
       System.exit(1);
     }
   }
 }
예제 #11
0
  /**
   * Wait for 'count' messages on controlQueue before continuing. Called by a publisher to make sure
   * that subscribers have started before it begins publishing messages.
   *
   * <p>If controlQueue doesn't exist, the method throws an exception.
   *
   * @param prefix prefix (publisher or subscriber) to be displayed
   * @param controlQueueName name of control queue
   * @param count number of messages to receive
   */
  public static void receiveSynchronizeMessages(String prefix, String controlQueueName, int count)
      throws Exception {
    QueueConnectionFactory queueConnectionFactory = null;
    QueueConnection queueConnection = null;
    QueueSession queueSession = null;
    Queue controlQueue = null;
    QueueReceiver queueReceiver = null;

    try {
      queueConnectionFactory = SampleUtilities.getQueueConnectionFactory();
      queueConnection = queueConnectionFactory.createQueueConnection();
      queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      controlQueue = getQueue(controlQueueName, queueSession);
      queueConnection.start();
    } catch (Exception e) {
      System.out.println("Connection problem: " + e.toString());
      if (queueConnection != null) {
        try {
          queueConnection.close();
        } catch (JMSException ee) {
        }
      }
      throw e;
    }

    try {
      System.out.println(
          prefix
              + "Receiving synchronize messages from "
              + controlQueueName
              + "; count = "
              + count);
      queueReceiver = queueSession.createReceiver(controlQueue);
      while (count > 0) {
        queueReceiver.receive();
        count--;
        System.out.println(prefix + "Received synchronize message; expect " + count + " more");
      }
    } catch (JMSException e) {
      System.out.println("Exception occurred: " + e.toString());
      throw e;
    } finally {
      if (queueConnection != null) {
        try {
          queueConnection.close();
        } catch (JMSException e) {
        }
      }
    }
  }
  /** Riceve messaggi dalla destinazione finquando ricevi vale true. */
  protected void startReceiveMessages() {
    /* riceve finchè ricevi vale true */
    if (!this.receiving) {
      try {
        MessageConsumer messageConsumer = session.createConsumer(destination);
        TextListener textListener = new TextListener(this);
        messageConsumer.setMessageListener(textListener);
        /* avvia la ricezione di messaggi per la connessione */
        connection.start();
        this.receiving = true;

      } catch (JMSException e) {
        System.out.println("JMSChatSubscriber: " + e.toString());
        System.exit(1);
      }
    }
  }
예제 #13
0
  /**
   * Send a message to controlQueue. Called by a subscriber to notify a publisher that it is ready
   * to receive messages.
   *
   * <p>If controlQueue doesn't exist, the method throws an exception.
   *
   * @param prefix prefix (publisher or subscriber) to be displayed
   * @param controlQueueName name of control queue
   */
  public static void sendSynchronizeMessage(String prefix, String controlQueueName)
      throws Exception {
    QueueConnectionFactory queueConnectionFactory = null;
    QueueConnection queueConnection = null;
    QueueSession queueSession = null;
    Queue controlQueue = null;
    QueueSender queueSender = null;
    TextMessage message = null;

    try {
      queueConnectionFactory = SampleUtilities.getQueueConnectionFactory();
      queueConnection = queueConnectionFactory.createQueueConnection();
      queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      controlQueue = getQueue(controlQueueName, queueSession);
    } catch (Exception e) {
      System.out.println("Connection problem: " + e.toString());
      if (queueConnection != null) {
        try {
          queueConnection.close();
        } catch (JMSException ee) {
        }
      }
      throw e;
    }

    try {
      queueSender = queueSession.createSender(controlQueue);
      message = queueSession.createTextMessage();
      message.setText("synchronize");
      System.out.println(prefix + "Sending synchronize message to " + controlQueueName);
      queueSender.send(message);
    } catch (JMSException e) {
      System.out.println("Exception occurred: " + e.toString());
      throw e;
    } finally {
      if (queueConnection != null) {
        try {
          queueConnection.close();
        } catch (JMSException e) {
        }
      }
    }
  }
    @Override
    public void onMessage(final Message msg) {
      // preserve the first error
      if (failed) {
        return;
      }

      try {
        TextMessage tm = (TextMessage) msg;

        if (!("message" + c).equals(tm.getText())) {
          // Failed
          failed = true;
          setError("Listener was supposed to get " + "message" + c + " but got " + tm.getText());
          latch.countDown();
        }

        c++;

        if (c % 500 == 0) {
          sess.commit();
        }

        if (c == num) {
          sess.commit();
          latch.countDown();
        }
      } catch (JMSException e) {
        e.printStackTrace();

        // Failed
        failed = true;
        setError("Listener got exception " + e.toString());
        latch.countDown();
      }
    }
  /** manually say OK I'm ready to take the message on the client */
  public void doReceiveAck() {

    if (doSetup()) {
      try {
        queueConnection = queueConnectionFactory.createQueueConnection();
        QueueSession queueSession =
            queueConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        queueConnection.start();
        String info = null;
        while (true) {
          Message m = queueReceiver.receive(1);
          if (m != null) {
            if (m instanceof TextMessage) {
              TextMessage message = (TextMessage) m;
              info = message.getText();

              if (info.indexOf("12") > -1) {
                log.debug("Skipping message:==> " + message.getText());
              } else {
                log.debug("Reading message:==> " + message.getText());
                message.acknowledge();
              }

            } else {
              break;
            }
          }
        }
      } catch (JMSException e) {
        log.error("Listen Exception occurred: " + e.toString());
      } finally {
        doCleanup();
      }
    }
  }
예제 #16
0
  public static void main(String[] args) {
    MapMessage outMessage;
    JMSConsumer orderConfirmReceiver;
    MapMessage inMessage;

    if (args.length != 1) {
      System.out.println("Error: Program takes numerical argument.");
      System.exit(1);
    }

    quantity = (new Integer(args[0])).intValue();
    System.out.println("Retailer: Quantity to be ordered is " + quantity);
    /*
     * Create non-transacted context and sender for
     * vendor order queue.
     * Create message to vendor, setting item and
     * quantity values.
     * Send message.
     * Create receiver for retailer confirmation queue.
     * Get message and report result.
     * Send an end-of-message-stream message so vendor
     * will stop processing orders.
     */
    try (JMSContext context = connectionFactory.createContext(); ) {
      outMessage = context.createMapMessage();
      outMessage.setString("Item", "Computer(s)");
      outMessage.setInt("Quantity", quantity);
      outMessage.setJMSReplyTo(retailerConfirmQueue);
      context.createProducer().send(vendorOrderQueue, outMessage);
      System.out.println("Retailer: Ordered " + quantity + " computer(s)");

      orderConfirmReceiver = context.createConsumer(retailerConfirmQueue);
      inMessage = (MapMessage) orderConfirmReceiver.receive();

      if (inMessage.getBoolean("OrderAccepted") == true) {
        System.out.println("Retailer: Order filled");
      } else {
        System.out.println("Retailer: Order not filled");
      }

      System.out.println("Retailer: Placing another order");
      outMessage.setInt("Quantity", quantity * 2);
      context.createProducer().send(vendorOrderQueue, outMessage);
      System.out.println("Retailer: Ordered " + outMessage.getInt("Quantity") + " computer(s)");
      inMessage = (MapMessage) orderConfirmReceiver.receive();

      if (inMessage.getBoolean("OrderAccepted") == true) {
        System.out.println("Retailer: Order filled");
      } else {
        System.out.println("Retailer: Order not filled");
      }

      /*
       * Send a non-text control message indicating end
       * of messages.
       */
      context.createProducer().send(vendorOrderQueue, context.createMessage());
    } catch (JMSException e) {
      System.err.println("Retailer: Exception occurred: " + e.toString());
    }
  }
예제 #17
0
  /**
   * Main method.
   *
   * @param args the destination name and type used by the example
   */
  public static void main(String[] args) {
    String destType;
    Destination dest = null;
    JMSConsumer consumer;

    if (args.length != 1) {
      System.err.println("Program takes one argument: <dest_type>");
      System.exit(1);
    }

    destType = args[0];
    System.out.println("Destination type is " + destType);

    if (!(destType.equals("queue") || destType.equals("topic"))) {
      System.err.println("Argument must be \"queue\" or \"topic\"");
      System.exit(1);
    }

    try {
      if (destType.equals("queue")) {
        dest = (Destination) queue;
      } else {
        dest = (Destination) topic;
      }
    } catch (JMSRuntimeException e) {
      System.err.println("Error setting destination: " + e.toString());
      System.exit(1);
    }

    /*
     * In a try-with-resources block, create context.
     * Create consumer.
     * Receive all text messages from destination until
     * a non-text message is received indicating end of
     * message stream.
     */
    try (JMSContext context = connectionFactory.createContext(); ) {
      consumer = context.createConsumer(dest);
      int count = 0;

      while (true) {
        Message m = consumer.receive(1000);

        if (m != null) {
          if (m instanceof TextMessage) {
            // Comment out the following two lines to receive
            // a large volume of messages
            System.out.println("Reading message: " + m.getBody(String.class));
            count += 1;
          } else {
            break;
          }
        }
      }
      System.out.println("Messages received: " + count);
    } catch (JMSException e) {
      System.err.println("Exception occurred: " + e.toString());
      System.exit(1);
    }
    System.exit(0);
  }