@org.junit.Ignore("AS7-2480")
  @Test
  public void testMessageMovement() throws Exception {

    MessageProducer producer = session.createProducer(queue);
    Message msgA = session.createTextMessage("A");
    producer.send(msgA);
    producer.send(session.createTextMessage("B"));
    producer.send(session.createTextMessage("C"));

    ModelNode op = getQueueOperation("move-message");
    op.get("message-id").set(msgA.getJMSMessageID());
    op.get("other-queue-name").set(getOtherQueueName());

    ModelNode result = execute(op, true);
    Assert.assertTrue(result.isDefined());
    Assert.assertTrue(result.asBoolean());

    result = execute(getQueueOperation("count-messages"), true);
    Assert.assertTrue(result.isDefined());
    Assert.assertEquals(2, result.asInt());

    op = getQueueOperation("move-messages");
    op.get("other-queue-name").set(getOtherQueueName());

    result = execute(op, true);
    Assert.assertTrue(result.isDefined());
    Assert.assertEquals(2, result.asInt());

    result = execute(getQueueOperation("count-messages"), true);
    Assert.assertTrue(result.isDefined());
    Assert.assertEquals(0, result.asInt());
  }
  @Test
  public void testChangeMessagePriority() throws Exception {

    MessageProducer producer = session.createProducer(queue);
    Message msgA = session.createTextMessage("A");
    producer.send(msgA);
    producer.send(session.createTextMessage("B"));
    producer.send(session.createTextMessage("C"));

    Set<Integer> priorities = new HashSet<Integer>();
    ModelNode result = execute(getQueueOperation("list-messages"), true);
    Assert.assertEquals(3, result.asInt());
    for (ModelNode node : result.asList()) {
      priorities.add(node.get("JMSPriority").asInt());
    }
    int newPriority = -1;
    for (int i = 0; i < 10; i++) {
      if (!priorities.contains(i)) {
        newPriority = i;
        break;
      }
    }

    ModelNode op = getQueueOperation("change-message-priority");
    op.get("message-id").set(msgA.getJMSMessageID());
    op.get("new-priority").set(newPriority);

    result = execute(op, true);
    Assert.assertTrue(result.isDefined());
    Assert.assertTrue(result.asBoolean());

    result = execute(getQueueOperation("list-messages"), true);
    boolean found = false;
    for (ModelNode node : result.asList()) {
      if (msgA.getJMSMessageID().equals(node.get("JMSMessageID").asString())) {
        Assert.assertEquals(newPriority, node.get("JMSPriority").asInt());
        found = true;
        break;
      }
    }
    Assert.assertTrue(found);

    op = getQueueOperation("change-messages-priority");
    op.get("new-priority").set(newPriority);

    result = execute(op, true);
    Assert.assertTrue(result.isDefined());
    Assert.assertTrue(result.asInt() > 1 && result.asInt() < 4);

    result = execute(getQueueOperation("list-messages"), true);
    for (ModelNode node : result.asList()) {
      Assert.assertEquals(newPriority, node.get("JMSPriority").asInt());
    }
  }
  public void onMessage(Message inMessage) {
    TextMessage msg = null;

    try {
      if (inMessage instanceof TextMessage) {
        msg = (TextMessage) inMessage;
        System.out.println("MESSAGE BEAN: Message received: " + msg.getText());
        long sleepTime = msg.getLongProperty("sleeptime");
        System.out.println("Sleeping for : " + sleepTime + " milli seconds ");
        Thread.sleep(sleepTime);
        queueConnection = queueConnectionFactory.createQueueConnection();
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueSender = queueSession.createSender(queue);
        TextMessage message = queueSession.createTextMessage();

        message.setText("REPLIED:" + msg.getText());
        message.setIntProperty("replyid", msg.getIntProperty("id"));
        System.out.println("Sending message: " + message.getText());
        queueSender.send(message);
      } else {
        System.out.println("Message of wrong type: " + inMessage.getClass().getName());
      }
    } catch (JMSException e) {
      e.printStackTrace();
    } catch (Throwable te) {
      te.printStackTrace();
    } finally {
      try {
        queueSession.close();
        queueConnection.close();
      } catch (Exception e) {
      }
    }
  } // onMessage
Exemple #4
0
  public static void main(String[] args) {
    try { // Create and start connection
      InitialContext ctx = new InitialContext();
      QueueConnectionFactory f = (QueueConnectionFactory) ctx.lookup("myQueueConnectionFactory");
      QueueConnection con = f.createQueueConnection();
      con.start();
      // 2) create queue session
      QueueSession ses = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      // 3) get the Queue object
      Queue t = (Queue) ctx.lookup("myQueue");
      // 4)create QueueSender object
      QueueSender sender = ses.createSender(t);
      // 5) create TextMessage object
      TextMessage msg = ses.createTextMessage();

      // 6) write message
      BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
      while (true) {
        System.out.println("Enter Msg, end to terminate:");
        String s = b.readLine();
        if (s.equals("end")) break;
        msg.setText(s);
        // 7) send message
        sender.send(msg);
        System.out.println("Message successfully sent.");
      }
      // 8) connection close
      con.close();
    } catch (Exception e) {
      System.out.println(e);
    }
  }
 private static void writeMessage(
     String message, QueueSession session, QueueSender sender, long seqNo) throws JMSException {
   System.out.println("Message is:" + message);
   Message reqMes = session.createTextMessage(message);
   reqMes.setLongProperty("SequenceNumber", seqNo);
   sender.send(reqMes);
 }
  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();
      }
    }
  }
  @AroundInvoke
  public Object checkArguments(InvocationContext ctx) throws Exception {
    try {
      log = Logger.getLogger(LogoutInterceptor.class);
      Object[] args = ctx.getParameters();
      String className = ctx.getTarget().getClass().getSimpleName();
      log.trace("Class name: " + className);
      String methodName = ctx.getMethod().getName();
      log.trace("Method: " + methodName);

      String sessionId = (String) args[0];
      if ((sessionId == null) || (sessionId.length() == 0)) {
        throw new Exception("sessionId should not be null");
      }

      cf = (QueueConnectionFactory) new InitialContext().lookup(QueueNames.CONNECTION_FACTORY);
      queue = (Queue) new InitialContext().lookup(QueueNames.LOGOUT_QUEUE);
      log.trace("Queue logout: " + queue.getQueueName());
      QueueConnection connection = cf.createQueueConnection();
      QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      QueueSender sender = session.createSender(queue);

      Message logoutMessage = session.createTextMessage(sessionId);
      Timestamp time = new Timestamp(new Date().getTime());
      // Messages will not accept timestamp property- must change to string
      logoutMessage.setStringProperty(PropertyNames.TIME, time.toString());
      sender.send(logoutMessage);
      session.close();

    } catch (Exception e) {
      log.fatal("Error in LogoutInterceptor", e);
    }
    return ctx.proceed();
  }
  @Test
  public void testMessageCounters() throws Exception {

    MessageProducer producer = session.createProducer(queue);
    producer.send(session.createTextMessage("A"));
    producer.send(session.createTextMessage("B"));

    ModelNode result = execute(getQueueOperation("list-message-counter-as-json"), true);
    Assert.assertTrue(result.isDefined());
    Assert.assertEquals(ModelType.STRING, result.getType());

    result = execute(getQueueOperation("list-message-counter-as-html"), true);
    Assert.assertTrue(result.isDefined());
    Assert.assertEquals(ModelType.STRING, result.getType());

    result = execute(getQueueOperation("list-message-counter-history-as-json"), true);
    Assert.assertTrue(result.isDefined());
    Assert.assertEquals(ModelType.STRING, result.getType());

    result = execute(getQueueOperation("list-message-counter-history-as-html"), true);
    Assert.assertTrue(result.isDefined());
    Assert.assertEquals(ModelType.STRING, result.getType());

    result = execute(getQueueOperation("reset-message-counter"), true);
    Assert.assertFalse(result.isDefined());

    result = execute(getQueueOperation("list-message-counter-history-as-json"), true);
    Assert.assertTrue(result.isDefined());
    Assert.assertEquals(ModelType.STRING, result.getType());
  }
  /**
   * Envia un mensaje a JMS.
   *
   * @param mensaje
   */
  public void send(String mensaje) throws Exception {
    try {

      QueueSender queueSender = queueSession.createSender(cola);
      TextMessage textMessage = queueSession.createTextMessage(mensaje);
      queueSender.send(textMessage);
      queueSender.close();

    } catch (JMSException e) {
      e.printStackTrace();
    } finally {
      c.close();
    }
  }
  @Test
  public void testListAndCountMessages() throws Exception {

    MessageProducer producer = session.createProducer(queue);
    producer.send(session.createTextMessage("A"));
    producer.send(session.createTextMessage("B"));

    ModelNode result = execute(getQueueOperation("list-messages"), true);
    Assert.assertTrue(result.isDefined());
    Assert.assertEquals(2, result.asList().size());

    result = execute(getQueueOperation("count-messages"), true);
    Assert.assertTrue(result.isDefined());
    Assert.assertEquals(2, result.asInt());
  }
 @AroundInvoke
 public Object log(InvocationContext context) throws Exception {
   System.out.println("---" + context.getMethod());
   QueueConnection conn = qcf.createQueueConnection();
   conn.start();
   QueueSession session = conn.createQueueSession(true, Session.SESSION_TRANSACTED);
   TextMessage msg = session.createTextMessage();
   msg.setText(
       context.getMethod().getDeclaringClass().getSimpleName()
           + ";"
           + context.getMethod().getName()
           + ";"
           + sessionContext.getCallerPrincipal().getName());
   QueueSender queueSender = session.createSender(queue);
   queueSender.send(msg);
   return context.proceed();
 }
Exemple #12
0
  public void onMessage(Message message) {

    try {
      boolean accepted = false;

      // Get the data from the message
      MapMessage msg = (MapMessage) message;
      double salary = msg.getDouble("Salary");
      double loanAmt = msg.getDouble("LoanAmount");

      // Determine whether to accept or decline the loan
      if (loanAmt < 200000) {
        accepted = (salary / loanAmt) > .25;
      } else {
        accepted = (salary / loanAmt) > .33;
      }
      System.out.println(
          ""
              + "Percent = "
              + (salary / loanAmt)
              + ", loan is "
              + (accepted ? "Accepted!" : "Declined"));

      // Send the results back to the borrower
      TextMessage tmsg = qSession.createTextMessage();
      tmsg.setText(accepted ? "Accepted!" : "Declined");

      // correlation
      tmsg.setJMSCorrelationID(message.getJMSMessageID());

      // Create the sender and send the message
      qSender = qSession.createSender((Queue) message.getJMSReplyTo());
      qSender.send(tmsg);

      System.out.println("\nWaiting for loan requests...");

    } catch (JMSException jmse) {
      jmse.printStackTrace();
      System.exit(1);
    } catch (Exception jmse) {
      jmse.printStackTrace();
      System.exit(1);
    }
  }
  /**
   * 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) {
        }
      }
    }
  }
Exemple #14
0
  public void onMessage(Message message) {

    try {
      boolean accepted = false;

      // Get the data from the message
      MapMessage msg = (MapMessage) message;
      double salary = msg.getDouble("Salary");
      double expAmt = msg.getDouble("Years’ experience");

      // Determine whether to accept or decline the loan
      if (expAmt < 200000) {
        accepted = (salary / expAmt) > .25;
      } else {
        accepted = (salary / expAmt) > .33;
      }
      if (salary <= 32000) {
        accepted = true;
      } else {
        if (expAmt == 0) accepted = false;
        else accepted = ((double) (expAmt - 32000) / expAmt) < 3000.;
      }
      System.out.println(" Salary proposal is " + (accepted ? "Accepted!" : "Declined"));

      // Send the results back to the borrower
      TextMessage tmsg = qSession.createTextMessage();
      tmsg.setText(accepted ? "Accepted!" : "Declined");
      tmsg.setJMSCorrelationID(message.getJMSMessageID());

      // Create the sender and send the message
      QueueSender qSender = qSession.createSender((Queue) message.getJMSReplyTo());
      qSender.send(tmsg);

      System.out.println("\nWaiting for salary requests...");

    } catch (JMSException jmse) {
      jmse.printStackTrace();
      System.exit(1);
    } catch (Exception jmse) {
      jmse.printStackTrace();
      System.exit(1);
    }
  }
  public static void main(String[] args) throws Exception {
    ActiveMQConnectionFactory connectionFactory =
        new ActiveMQConnectionFactory("admin", "admin", "tcp://localhost:61616");
    QueueConnection queueConnection = connectionFactory.createQueueConnection();

    QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue("test-queue");

    MessageProducer producer = queueSession.createProducer(queue);

    queueConnection.start();

    TextMessage textMessage =
        queueSession.createTextMessage("Current time: " + System.currentTimeMillis());

    producer.send(textMessage);
    log.info("Message sent at {}", System.currentTimeMillis());

    producer.close();
    queueSession.close();
    queueConnection.close();
  }
Exemple #16
0
  public static void queueSend(
      QueueConnection cnn,
      String queueName,
      String payload,
      boolean transacted,
      int ack,
      String replyTo)
      throws JMSException {
    QueueSession session = cnn.createQueueSession(transacted, ack);
    Queue queue = session.createQueue(queueName);
    QueueSender sender = session.createSender(queue);
    TextMessage msg = session.createTextMessage();
    msg.setText(payload);
    msg.setJMSDeliveryMode(ack);
    if (replyTo != null) {
      msg.setJMSReplyTo(session.createQueue(replyTo));
    }

    sender.send(msg);
    sender.close();
    session.close();
  }
  @Override
  public boolean runExample() throws Exception {
    QueueConnection connection = null;
    InitialContext initialContext = null;
    try {
      // Step 1. Create an initial context to perform the JNDI lookup.
      initialContext = getContext(0);

      // Step 2. Perfom a lookup on the queue
      Queue queue = (Queue) initialContext.lookup("/queue/exampleQueue");

      // Step 3. Look-up the JMS queue connection factory
      QueueConnectionFactory cf =
          (QueueConnectionFactory) initialContext.lookup("/ConnectionFactory");

      // Step 4. Create a TextReverserService which consumes messages from the queue and sends
      // message with reversed
      // text
      TextReverserService reverserService = new TextReverserService(cf, queue);

      // Step 5. Create a JMS QueueConnection
      connection = cf.createQueueConnection();

      // Step 6. Start the connection
      connection.start();

      // Step 7. Create a JMS queue session with AUTO_ACKNOWLEDGE mode
      QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

      // Step 8. Create a JMS queue requestor to send requests to the queue
      QueueRequestor queueRequestor = new QueueRequestor(session, queue);

      // Step 9. Create a JMS message to send as a request
      TextMessage request = session.createTextMessage("Hello, World!");

      // Step 10. Use the requestor to send the request and wait to receive a reply
      TextMessage reply = (TextMessage) queueRequestor.request(request);

      // Step 11. The reply's text contains the reversed request's text
      System.out.println("Send request: " + request.getText());
      System.out.println("Received reply:" + reply.getText());

      // Step.12 close the queue requestor
      queueRequestor.close();

      // Step 13. close the text reverser service
      reverserService.close();

      return true;
    } finally {
      if (connection != null) {
        try {
          // Step 14. Be sure to close the JMS resources!
          connection.close();
        } catch (JMSException e) {
          e.printStackTrace();
        }
      }

      if (initialContext != null) {
        // Also the InitialContext
        initialContext.close();
      }
    }
  }
Exemple #18
0
 public void send() throws JMSException {
   testSender.send(queueSession.createTextMessage("message for queue"));
 }
Exemple #19
0
 private TextMessage createTextMessage(String text) throws JMSException {
   return qSess.createTextMessage(text);
 }