/**
   * Verifies the MessageDrivenContext methods.
   *
   * @param msg message
   */
  public void onMessage(final Message msg) {
    String op = null;

    try {
      op = msg.getStringProperty(MessageProperty.OPERATION.toString());
    } catch (JMSException e) {
      logger.debug("Error getting operation type: {0}", e);
    }

    if (ctx == null) {
      logger.debug("The SessionContext reference is null.");
    } else if (isEqual(GET_CALLER_PRINCIPAL, op)) {

      // TODO: test case

    } else if (isEqual(GET_ROLLBACK_ONLY, op)) {
      try {
        ctx.getRollbackOnly();
      } catch (IllegalStateException e) {
        log(
            MDBBeanManagedContext.class,
            ON_MESSAGE,
            MDBBeanManagedContext.class,
            GET_ROLLBACK_ONLY);
      } catch (Exception e) {
        logger.debug("Unexpected error.", e);
      }

    } else if (isEqual(SET_ROLLBACK_ONLY, op)) {
      try {
        ctx.setRollbackOnly();
      } catch (IllegalStateException e) {
        log(
            MDBBeanManagedContext.class,
            ON_MESSAGE,
            MDBBeanManagedContext.class,
            SET_ROLLBACK_ONLY);
      }

    } else if (isEqual(TIMER, op)) {
      try {
        checkInstance(ctx.getTimerService());
        log(MDBBeanManagedContext.class, ON_MESSAGE, MDBBeanManagedContext.class, TIMER);
      } catch (Exception e) {
        logger.debug("Unexpected error.", e);
      }

    } else if (isEqual(USER_TRANSACTION, op)) {
      try {
        checkInstance(ctx.getTimerService());
        log(MDBBeanManagedContext.class, ON_MESSAGE, MDBBeanManagedContext.class, USER_TRANSACTION);
      } catch (Exception e) {
        logger.debug("Unexpected error.", e);
      }
    }
  }
Beispiel #2
0
  public void onMessage(Message message) {
    try {
      logger.info("onMessage called");
      ObjectMessage objectMessage = (ObjectMessage) message;
      ImportRequest request = (ImportRequest) objectMessage.getObject();

      ImportResult dbResult = importLogger.getImportResult(request.getResult().getJobId());
      if (dbResult.getStatusMessage() == null || dbResult.getStatusMessage().length() == 0) {
        processImportRequest(request);
      } else {
        logger.info("Message already failed! Skipping message");
      }
    } catch (JMSException e) {
      logger.error("Error processing message", e);
      context.setRollbackOnly();
    } catch (Exception e) {
      logger.info("onMessage caught exception");
      context.setRollbackOnly();
    }
  }
 /**
  * Listens for messages arriving in the JMS queue and then acts on the presence by saving the
  * message in a database. This could easily be changed to do something else with the info.
  *
  * @param message - a JMS representation of the data.
  */
 @Override
 public void onMessage(Message message) {
   ObjectMessage msg = null;
   try {
     if (message instanceof ObjectMessage) {
       msg = (ObjectMessage) message;
       NewsEntity e = (NewsEntity) msg.getObject();
       save(e);
     }
   } catch (JMSException e) {
     e.printStackTrace();
     mdc.setRollbackOnly();
   } catch (Throwable te) {
     te.printStackTrace();
   }
 }
Beispiel #4
0
  public static void msgReceived(
      Message msg,
      AtomicInteger counter,
      ConnectionFactory connectionFactory,
      MessageDrivenContext sessionContext,
      Topic topic,
      Class clazz) {
    int value = counter.incrementAndGet();
    if (value % 100 == 0) {
      System.out.println("Received " + value + " on " + clazz.getSimpleName());
    }

    int i = 0;
    try {
      i = msg.getIntProperty("i");
    } catch (Throwable e) {
      e.printStackTrace();
      i = 1000;
    }

    try {
      if (i < 100) {
        System.out.println(clazz.getSimpleName() + " waiting 1 second on " + value);
        Thread.sleep(1000);
      } else {
        // Thread.sleep(randomLong() % 100);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      Connection conn = connectionFactory.createConnection();
      Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
      MessageProducer prod = sess.createProducer(topic);
      TextMessage outMessage = sess.createTextMessage("hello");
      outMessage.setIntProperty("receiver", 100);
      prod.send(outMessage);
      conn.close();
    } catch (JMSException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      sessionContext.setRollbackOnly();
    }
  }