/**
   * 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();
    }
  }
  /**
   * Verifies if the operations allowed for Message Driven listener methods are working properly.
   *
   * @param message msg
   */
  public void onMessage(final Message message) {
    String op = null;

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

    if (isEqual(RESOURCE_MANAGER, op)) {
      DataSource ds = (DataSource) ctx.lookup("jdbc/ds01");
      checkResource(ds);
      log(
          MDBListenerMethodAccess.class,
          ON_MESSAGE,
          MDBListenerMethodAccess.class,
          RESOURCE_MANAGER);

    } else if (isEqual(ENTERPRISE_BEAN, op)) {
      ItfOneMethod01 bean = (ItfOneMethod01) ctx.lookup("ejb/bean01");
      bean.getBool();
      log(
          MDBListenerMethodAccess.class,
          ON_MESSAGE,
          MDBListenerMethodAccess.class,
          ENTERPRISE_BEAN);

    } else if (isEqual(ENTITY_MANAGER_FACTORY, op)) {
      EntityManagerFactory emf = (EntityManagerFactory) ctx.lookup("persistence/pu01");
      try {
        EMFactoryTester.checkInstance(emf, "tmpTable" + this.hashCode());
        log(
            MDBListenerMethodAccess.class,
            ON_MESSAGE,
            MDBListenerMethodAccess.class,
            ENTITY_MANAGER_FACTORY);
      } catch (Exception e) {
        logger.debug("Error in EntityManagerFactory use: {0}", e);
        e.printStackTrace();
      }

    } else if (isEqual(ENTITY_MANAGER, op)) {
      EntityManager em = (EntityManager) ctx.lookup("persistence/pctx01");
      try {
        EntityManagerTester.checkInstance(em, "tmpTable" + this.hashCode());
        log(
            MDBListenerMethodAccess.class,
            ON_MESSAGE,
            MDBListenerMethodAccess.class,
            ENTITY_MANAGER);
      } catch (Exception e) {
        logger.debug("Error in EntityManagerFactory use: {0}", e);
        e.printStackTrace();
      }

    } else if (isEqual(TIMER, op)) {
      timer.hashCode();
      log(MDBListenerMethodAccess.class, ON_MESSAGE, MDBListenerMethodAccess.class, TIMER);

    } else if (isEqual(USER_TRANSACTION, op)) {
      if (utx == null) {
        log(
            MDBListenerMethodAccess.class,
            ON_MESSAGE,
            MDBListenerMethodAccess.class,
            USER_TRANSACTION);
      }
    }
  }
Beispiel #6
0
 @Command
 public String whoami() {
   return context.getCallerPrincipal() == null
       ? "Unknown"
       : context.getCallerPrincipal().getName();
 }