Ejemplo n.º 1
0
 public Destination resolveDestinationName(
     Session session, String destinationName, boolean pubSubDomain) throws JMSException {
   // use a temporary queue to gather the reply message
   synchronized (refreshWanted) {
     if (queue == null || refreshWanted.compareAndSet(true, false)) {
       queue = session.createTemporaryQueue();
       setReplyTo(queue);
       if (log.isDebugEnabled()) {
         log.debug("Refreshed Temporary ReplyTo Queue. New queue: {}", queue.getQueueName());
       }
       refreshWanted.notifyAll();
     }
   }
   return queue;
 }
Ejemplo n.º 2
0
  /**
   * Example for sending a message fire and forget
   *
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    final String methodName = "doGet";

    // We simply return some text showing what we've done
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();

    WLMJMSMessageProducer wlmConnection = null;
    boolean complete = false;
    try {
      // Begin a transaction
      userTransaction.begin();

      // Print the user transaction status
      out.println(WLMJMSTranUtils.summarizeUserTranStatus(userTransaction));

      // Create our JMS connection/session/producer using the WLM connection logic
      // Many fire-and-forget scenarios are for exactly once delivery, where a
      // database is updated in the same transaction as sending the request.
      // For this reason in this example we begin a transaction and send
      // a persistent message.
      // As such JDBC operations could be inserted into this sample, and would
      // be coordinated in an atomic transaction with sending the request.
      //
      // If you are instead looking to send nonpersistent messages, such as publishing
      // some non-critical state data on a topic, then you should consider
      // removing the transaction, changing the transaction
      // boolean in the getMessageProducer() call to false, and setting the DeliveryMode
      // to NON_PERSISTENT when sending the message.
      out.println("JMS destination: " + fireAndForgetTarget);
      wlmConnection =
          wlmJMSAttach.getMessageProducer(fireAndForgetTarget, true, Session.AUTO_ACKNOWLEDGE);

      // TODO: Replace this section with business logic
      // We create a temporary queue solely for the purpose of finding out where
      // we are connected, as the temporary queue name should show this.
      // This is inefficient, so it is for demonstration purposes only.
      TemporaryQueue temporaryQueue = wlmConnection.getSession().createTemporaryQueue();
      String temporaryQueueName = temporaryQueue.getQueueName();
      out.println("Temporary queue showing where we are connected:");
      out.println(temporaryQueueName);
      temporaryQueue.delete();
      String exampleMessageBody =
          this.getClass().getName()
              + " (thread \""
              + Thread.currentThread().getName()
              + "\") sending at "
              + new Date();
      Message message = wlmConnection.getSession().createTextMessage(exampleMessageBody);
      out.println("Sending message \"" + exampleMessageBody + "\"");

      // Send the message
      wlmConnection
          .getProducer()
          .send(
              message,
              DeliveryMode.PERSISTENT, /* We are persistent in this example */
              wlmConnection.getProducer().getPriority() /* Default priority */,
              0 /* Do not expire */);
      out.println("JMSMessageID: " + message.getJMSMessageID());

      // Commit the transaction
      userTransaction.commit();

      // Mark that we're complete, so that we throw any exception seen during cleanup,
      // and do not attempt rollback of the transaction
      complete = true;
    } catch (HeuristicMixedException e) {
      if (log.enabled()) log.logExStack(methodName, "Transaction HeuristicMixedException", e);
      throw new ServletException("Transaction HeuristicMixedException: " + e.getMessage(), e);
    } catch (HeuristicRollbackException e) {
      if (log.enabled()) log.logExStack(methodName, "Transaction HeuristicRollbackException", e);
      throw new ServletException("Transaction HeuristicRollbackException: " + e.getMessage(), e);
    } catch (RollbackException e) {
      if (log.enabled()) log.logExStack(methodName, "Transaction RollbackException", e);
      throw new ServletException("Transaction RollbackException: " + e.getMessage(), e);
    } catch (NotSupportedException e) {
      if (log.enabled()) log.logExStack(methodName, "Transaction NotSupportedException", e);
      throw new ServletException("Transaction NotSupportedException: " + e.getMessage(), e);
    } catch (SystemException e) {
      if (log.enabled()) log.logExStack(methodName, "Transaction SystemException", e);
      throw new ServletException("Transaction SystemException: " + e.getMessage(), e);
    } catch (JMSException e) {
      if (log.enabled()) log.logRootExMsg(methodName, "JMSException in business logic", e);
      throw new ServletException("JMSException: " + e.getMessage(), e);
    } finally {
      // Rollback if we didn't complete
      if (!complete)
        try {
          userTransaction.rollback();
        } catch (SystemException e) {
          // We are already on an exception path, so we should not throw this exception
          if (log.enabled()) log.logExStack(methodName, "Exception during rollback", e);
        }

      // Close off our resources, ensuring we only throw an exception if we completed
      // our earlier logic, so we don't override an earlier exception that being thrown.
      if (wlmConnection != null)
        try {
          wlmConnection.close(complete);
        } catch (JMSException e) {
          throw new ServletException("JMSException on Connection close: " + e.getMessage(), e);
        }
    }
  }
Ejemplo n.º 3
0
  /**
   * Try to use ActiveMQ StatisticsPlugin to get size and if that fails fallback to {@link
   * JMSMailQueue#getSize()}
   */
  @Override
  public long getSize() throws MailQueueException {

    Connection connection = null;
    Session session = null;
    MessageConsumer consumer = null;
    MessageProducer producer = null;
    TemporaryQueue replyTo = null;
    long size;

    try {
      connection = connectionFactory.createConnection();
      connection.start();

      session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      replyTo = session.createTemporaryQueue();
      consumer = session.createConsumer(replyTo);

      Queue myQueue = session.createQueue(queueName);
      producer = session.createProducer(null);

      String queueName = "ActiveMQ.Statistics.Destination." + myQueue.getQueueName();
      Queue query = session.createQueue(queueName);

      Message msg = session.createMessage();
      msg.setJMSReplyTo(replyTo);
      producer.send(query, msg);
      MapMessage reply = (MapMessage) consumer.receive(2000);
      if (reply != null && reply.itemExists("size")) {
        try {
          size = reply.getLong("size");
          return size;
        } catch (NumberFormatException e) {
          // if we hit this we can't calculate the size so just catch
          // it
        }
      }

    } catch (Exception e) {
      throw new MailQueueException("Unable to remove mails", e);

    } finally {

      if (consumer != null) {

        try {
          consumer.close();
        } catch (JMSException e1) {
          e1.printStackTrace();
          // ignore on rollback
        }
      }

      if (producer != null) {

        try {
          producer.close();
        } catch (JMSException e1) {
          // ignore on rollback
        }
      }

      if (replyTo != null) {
        try {

          // we need to delete the temporary queue to be sure we will
          // free up memory if thats not done and a pool is used
          // its possible that we will register a new mbean in jmx for
          // every TemporaryQueue which will never get unregistered
          replyTo.delete();
        } catch (JMSException e) {
        }
      }
      try {
        if (session != null) session.close();
      } catch (JMSException e1) {
        // ignore here
      }

      try {
        if (connection != null) connection.close();
      } catch (JMSException e1) {
        // ignore here
      }
    }

    // if we came to this point we should just fallback to super method
    return super.getSize();
  }