Ejemplo n.º 1
2
 /**
  * Creates all the necessary objects for receiving messages from a JMS queue.
  *
  * @param ctx JNDI initial context
  * @param queueName name of queue
  * @exception NamingException if operation cannot be performed
  * @exception JMSException if JMS fails to initialize due to internal error
  */
 public void init(Context ctx, String queueName) throws NamingException, JMSException {
   qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
   qcon = qconFactory.createQueueConnection();
   qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
   queue = (Queue) ctx.lookup(queueName);
   qreceiver = qsession.createReceiver(queue);
   qreceiver.setMessageListener(this);
   qcon.start();
 }
Ejemplo n.º 2
0
 public static QueueReceiver getQueueReceiver(QueueConnection cnn, String queueName)
     throws JMSException {
   QueueSession session = cnn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
   Queue queue = session.createQueue(queueName);
   QueueReceiver receiver = session.createReceiver(queue);
   return receiver;
 }
  /** 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();
      }
    }
  }
Ejemplo n.º 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);
    }
  }
  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();
      }
    }
  }
Ejemplo n.º 6
0
  public QLender(String queuecf, String requestQueue) {
    try {
      // Connect to the provider and get the JMS connection
      Context ctx = new InitialContext();
      QueueConnectionFactory qFactory = (QueueConnectionFactory) ctx.lookup(queuecf);
      qConnect = qFactory.createQueueConnection();

      // Create the JMS Session
      qSession = qConnect.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

      // Lookup the request queue
      requestQ = (Queue) ctx.lookup(requestQueue);

      // Now that setup is complete, start the Connection
      qConnect.start();

      // Create the message listener
      QueueReceiver qReceiver = qSession.createReceiver(requestQ);
      qReceiver.setMessageListener(this);

      System.out.println("Waiting for loan requests...");

    } catch (JMSException jmse) {
      jmse.printStackTrace();
      System.exit(1);
    } catch (NamingException jne) {
      jne.printStackTrace();
      System.exit(1);
    }
  }
  /** Constructor del encargado de enviar mensajes JMS. */
  public RespuestaConsultaValue() {
    try {
      final Properties env = new Properties();
      env.put("org.jboss.ejb.client.scoped.context", true);
      env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
      env.put("endpoint.name", "endpoint-client");

      ictx = new InitialContext(env);

      Object tmp = ictx.lookup("ConnectionFactory");
      QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;
      conn = qcf.createQueueConnection();
      this.cola = (Queue) ictx.lookup("queue/test");

      queueSession = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

      conn.start();

    } catch (JMSException e) {
      e.printStackTrace();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  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
  @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();
  }
Ejemplo n.º 10
0
 private QueueSession getQueueSession() throws Exception {
   if (queueSession == null) {
     Context ctx = getJndiInitialContext();
     QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup(CONNECTION_FACTORY_NAME);
     QueueConnection conn = factory.createQueueConnection();
     conn.start();
     queueSession = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
   }
   return queueSession;
 }
Ejemplo n.º 11
0
 /** Initialize standard actors */
 public void init() throws Exception {
   if (!isBroker08()) {
     _queue = (Queue) getInitialContext().lookup(QUEUENAME);
     _queueFactory = getConnectionFactory();
     _xaqueueConnection = _queueFactory.createXAQueueConnection("guest", "guest");
     XAQueueSession session = _xaqueueConnection.createXAQueueSession();
     _queueConnection = _queueFactory.createQueueConnection("guest", "guest");
     _nonXASession = _queueConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
     init(session, _queue);
   }
 }
Ejemplo n.º 12
0
  @Test
  public void receiver() throws Exception {
    QueueConnection connection = null;
    QueueSession session = null;
    try {
      // 创建链接工厂
      QueueConnectionFactory factory =
          new ActiveMQConnectionFactory(
              ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);
      // 通过工厂创建一个连接
      connection = factory.createQueueConnection();
      // 启动连接
      connection.start();
      // 创建一个session会话
      session = connection.createQueueSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
      // 创建一个消息队列
      javax.jms.Queue queue = session.createQueue(TARGET);
      // 创建消息制作者
      javax.jms.QueueReceiver receiver = session.createReceiver(queue);

      receiver.setMessageListener(
          new MessageListener() {
            public void onMessage(Message msg) {
              if (msg != null) {
                // MapMessage map = (MapMessage) msg;
                try {
                  System.out.println(msg.getStringProperty("text"));

                  // System.out.println(map.getLong("time") + "接收#" +
                  // map.getString("text"));
                } catch (JMSException e) {
                  e.printStackTrace();
                }
              }
            }
          });
      // 休眠100ms再关闭
      Thread.sleep(1000 * 100);

      // 提交会话
      session.commit();

    } catch (Exception e) {
      throw e;
    } finally {
      // 关闭释放资源
      if (session != null) {
        session.close();
      }
      if (connection != null) {
        connection.close();
      }
    }
  }
Ejemplo n.º 13
0
 public static Message queueReceiver(QueueConnection cnn, String queueName, long timeout)
     throws JMSException {
   QueueSession session = cnn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
   Queue queue = session.createQueue(queueName);
   QueueReceiver receiver = session.createReceiver(queue);
   Message msg = receiver.receive(timeout);
   if (msg != null) msg.acknowledge();
   receiver.close();
   session.close();
   return msg;
 }
Ejemplo n.º 14
0
  protected synchronized void openConnection() {
    try {
      m_connection = m_factory.createQueueConnection();
      m_connection.start();

      m_session = m_connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

      m_sender = m_session.createSender(m_queue);
    } catch (final Exception e) {
      getErrorHandler().error("Error starting connection", e, null);
    }
  }
Ejemplo n.º 15
0
  private void setupMessageListener()
      throws javax.jms.JMSException, javax.naming.NamingException, Exception {
    InitialContext ctx = new InitialContext();

    QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("jms/taskQueueFactory");
    connection = factory.createQueueConnection();
    session = connection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
    Queue queue = (Queue) ctx.lookup("jms/taskQueue");

    receiver = session.createConsumer(queue);
    connection.start();
    ctx.close();
  }
Ejemplo n.º 16
0
  public void initialize() throws JMSException, NamingException {
    InitialContext iniCtx = new InitialContext();
    QueueConnectionFactory queueConnectionFactory =
        (QueueConnectionFactory) iniCtx.lookup("ConnectionFactory");

    connection = queueConnectionFactory.createQueueConnection();
    // connection = queueConnectionFactory.createQueueConnection(jmsSecurityPrincipal,
    // jmsSecurityCredential);

    queue = (Queue) iniCtx.lookup("queue/om.esb.jms.exception.persist.gw");
    session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
    connection.start();
    System.out.println("Connected");
  }
Ejemplo n.º 17
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) {
        }
      }
    }
  }
Ejemplo n.º 18
0
 public static void drainQueue(QueueConnection cnn, String queue) throws Exception {
   QueueSession session = cnn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
   Queue q = session.createQueue(queue);
   QueueReceiver receiver = session.createReceiver(q);
   // Add a delay so that activeMQ can fetch messages from the broker
   // Thread.sleep(5000);
   Message msg = null;
   while ((msg = receiver.receive(1000)) != null) {
     System.out.println("Removing message: " + msg);
     msg.acknowledge();
   }
   receiver.close();
   session.close();
 }
  public void execute(JobExecutionContext context) throws JobExecutionException {

    QueueConnectionFactory qcf = null;
    QueueConnection conn = null;
    QueueSession session = null;
    Queue queue = null;
    QueueSender sender = null;
    InitialContext ctx = null;

    final JobDetail detail = context.getJobDetail();
    final JobDataMap jobDataMap = detail.getJobDataMap();

    try {

      qcf =
          (QueueConnectionFactory)
              ctx.lookup(jobDataMap.getString(JmsHelper.JMS_CONNECTION_FACTORY_JNDI));
      ctx = JmsHelper.getInitialContext(jobDataMap);

      if (JmsHelper.isDestinationSecure(jobDataMap)) {
        String user = jobDataMap.getString(JmsHelper.JMS_USER);
        String pw = jobDataMap.getString(JmsHelper.JMS_PASSWORD);
        conn = qcf.createQueueConnection(user, pw);
      } else {
        conn = qcf.createQueueConnection();
      }

      boolean useTransactions = JmsHelper.useTransaction(jobDataMap);
      int ackMode = jobDataMap.getInt(JmsHelper.JMS_ACK_MODE);
      session = conn.createQueueSession(useTransactions, ackMode);
      String queueName = jobDataMap.getString(JmsHelper.JMS_DESTINATION_JNDI);
      queue = (Queue) ctx.lookup(queueName);
      sender = session.createSender(queue);
      String factoryClass = jobDataMap.getString(JmsHelper.JMS_MSG_FACTORY_CLASS_NAME);
      JmsMessageFactory factory = JmsHelper.getMessageFactory(factoryClass);
      Message m = factory.createMessage(jobDataMap, session);
      sender.send(m);
    } catch (NamingException e) {
      throw new JobExecutionException(e.getMessage());
    } catch (JMSException e) {
      throw new JobExecutionException(e.getMessage());
    } catch (JmsJobException e) {
      throw new JobExecutionException(e.getMessage());
    } finally {
      JmsHelper.closeResource(sender);
      JmsHelper.closeResource(session);
      JmsHelper.closeResource(conn);
    }
  }
  @Override
  public void setUp() throws Exception {
    super.setUp();
    try {
      queueConnection = queueConnectionFactory.createQueueConnection();
      queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      topicConnection = topicConnectionFactory.createTopicConnection();
      topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

      queueConnection.start();
      topicConnection.start();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Ejemplo n.º 21
0
 @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();
 }
Ejemplo n.º 22
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) {
        }
      }
    }
  }
  public static void main(String[] args) {
    try {
      QueueConnectionFactory queueConnectionFactory;
      QueueConnection queueConnection;
      QueueSession queueSession;
      QueueReceiver queueReceiver;
      Queue queue;
      TextMessage msg;

      // JNDI InitialContextを作成します
      InitialContext ctx = new InitialContext();
      // Connection FactoryとQueueをLook upします
      queueConnectionFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
      queue = (Queue) ctx.lookup(QUEUE);

      // コネクションを作成
      queueConnection = queueConnectionFactory.createQueueConnection();
      // セッションを作成
      queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      // キューレシーバーを作成
      queueReceiver = queueSession.createReceiver(queue);
      // メッセージの配送をスタート
      queueConnection.start();
      // メッセージの受信
      while (true) {
        Message m = queueReceiver.receive(1);
        if (m != null) {
          if (m instanceof TextMessage) {
            msg = (TextMessage) m;
            System.out.println(msg.getText());
          } else {
            break;
          }
        }
      }
      // 接続を切断
      queueReceiver.close();
      queueSession.close();
      queueConnection.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  @Test
  public void testListConsumers() throws Exception {

    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("host", TestSuiteEnvironment.getServerAddress());
    TransportConfiguration transportConfiguration =
        new TransportConfiguration(NettyConnectorFactory.class.getName(), map);
    HornetQConnectionFactory cf =
        HornetQJMSClient.createConnectionFactoryWithoutHA(
            JMSFactoryType.CF, transportConfiguration);
    cf.setClientID("consumer");
    consumerConn = cf.createQueueConnection("guest", "guest");
    consumerConn.start();
    consumerSession = consumerConn.createQueueSession(false, TopicSession.AUTO_ACKNOWLEDGE);

    ModelNode result = execute(getQueueOperation("list-consumers-as-json"), true);
    Assert.assertTrue(result.isDefined());
    Assert.assertEquals(ModelType.STRING, result.getType());
  }
Ejemplo n.º 25
0
  /**
   * Sends a message to a queue.
   *
   * @param queueName
   * @param key Optional.
   * @param obj Mandatory.
   */
  public void sendMsgToQueue(final String queueName, final String key, final Serializable obj) {
    log.debug("sendMsgToQueue(queueName, key, obj)::started");
    QueueConnection conn = null;
    QueueSession session = null;
    QueueSender sender = null;

    try {
      final String jndiName = "queue/" + queueName;
      final Context ctx = getInitialContext(null);
      final QueueConnectionFactory factory =
          (QueueConnectionFactory) ctx.lookup(CONNECTION_FACTORY);
      conn = factory.createQueueConnection();
      session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      final Queue queue = (Queue) ctx.lookup(jndiName);
      sender = session.createSender(queue);

      ObjectMessage message = session.createObjectMessage();
      if (key == null) {
        message.setObject((Serializable) obj);
      } else {
        message.setObjectProperty(key, obj);
      }

      sender.send(message);

    } catch (NamingException e) {
      throw new LocatorException(
          "A naming exception occured while trying to send message "
              + obj
              + " to queue "
              + queueName,
          e);
    } catch (JMSException e) {
      throw new LocatorException(
          "A JMS exception occured while trying to send message " + obj + " to queue " + queueName,
          e);
    } finally {
      log.debug("sendMsgToQueue(queueName, key, obj)::finished");
      close(sender);
      close(session);
      close(conn);
    }
  }
  @Before
  public void addTopic() throws Exception {

    count++;
    adminSupport.createJmsQueue(getQueueName(), getQueueJndiName());
    adminSupport.createJmsQueue(getOtherQueueName(), getOtherQueueJndiName());
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("host", TestSuiteEnvironment.getServerAddress());
    TransportConfiguration transportConfiguration =
        new TransportConfiguration(NettyConnectorFactory.class.getName(), map);
    HornetQConnectionFactory cf =
        HornetQJMSClient.createConnectionFactoryWithoutHA(
            JMSFactoryType.CF, transportConfiguration);
    cf.setClientID("sender");
    conn = cf.createQueueConnection("guest", "guest");
    conn.start();
    queue = HornetQJMSClient.createQueue(getQueueName());
    otherQueue = HornetQJMSClient.createQueue(getOtherQueueName());
    session = conn.createQueueSession(false, TopicSession.AUTO_ACKNOWLEDGE);
  }
  /** Tasks to be carried out in the STARTUP_EVENT. Logs a message */
  private void onStartTask() {
    ctx.log("LifecycleTopic: STARTUP_EVENT");
    // my code
    QueueSession qsession[] = new QueueSession[10];
    Queue queue[] = new Queue[10];

    try {
      for (int i = 0; i < 10; i++) {
        // Get initial context
        ctx.log("Get initial context");
        InitialContext initialContext = new InitialContext();

        // look up the connection factory from the object store
        ctx.log("Looking up the queue connection factory from JNDI");
        QueueConnectionFactory factory =
            (QueueConnectionFactory) initialContext.lookup("jms/QCFactory");

        // look up queue from the object store
        ctx.log("Create queue connection");
        QueueConnection qconn = factory.createQueueConnection();

        ctx.log("Create queue session");
        qsession[i] = qconn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

        ctx.log("Looking up the queue from JNDI");
        queue[i] = (Queue) initialContext.lookup("jms/SampleQueue");
      }

      updateDB();

    } catch (Exception e) {
      ctx.log("Exception caught in test code");
      e.printStackTrace();
    }

    // end my code

    // my code
    // createAccount();
    // end my code
  }
Ejemplo n.º 28
0
 /** @param failedMailId */
 public void callBack(String failedMailId) {
   try {
     Properties props = new Properties();
     props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
     props.setProperty("java.naming.provider.url", SenderConfig.getProp("callbackUrl"));
     props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
     InitialContext ctx = new InitialContext(props);
     QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
     QueueConnection queueConnection = factory.createQueueConnection();
     QueueSession queueSession =
         queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
     Queue queue = (Queue) ctx.lookup("queue/" + SenderConfig.getProp("callbackJNDI"));
     ObjectMessage objMsg = queueSession.createObjectMessage();
     objMsg.setObject(failedMailId);
     QueueSender queueSender = queueSession.createSender(queue);
     queueSender.send(objMsg);
     queueSession.close();
     queueConnection.close();
   } catch (Exception e) {
     log.error("sendMail/SendMailListener/Exception: [" + failedMailId + "]", e);
   }
 }
Ejemplo n.º 29
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();
  }
  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();
  }