public void testWithQueueConnection() throws JMSException {
    MockControl conControl = MockControl.createControl(QueueConnection.class);
    Connection con = (QueueConnection) conControl.getMock();

    con.start();
    conControl.setVoidCallable(1);
    con.stop();
    conControl.setVoidCallable(1);
    con.close();
    conControl.setVoidCallable(1);

    conControl.replay();

    SingleConnectionFactory scf = new SingleConnectionFactory(con);
    QueueConnection con1 = scf.createQueueConnection();
    con1.start();
    con1.stop(); // should be ignored
    con1.close(); // should be ignored
    QueueConnection con2 = scf.createQueueConnection();
    con2.start();
    con2.stop(); // should be ignored
    con2.close(); // should be ignored
    scf.destroy(); // should trigger actual close

    conControl.verify();
  }
  @After
  public void removeTopic() throws Exception {

    if (conn != null) {
      conn.stop();
    }
    if (session != null) {
      session.close();
    }
    if (conn != null) {
      conn.close();
    }

    if (consumerConn != null) {
      consumerConn.stop();
    }
    if (consumerSession != null) {
      consumerSession.close();
    }
    if (consumerConn != null) {
      consumerConn.close();
    }

    adminSupport.removeJmsQueue(getQueueName());
    adminSupport.removeJmsQueue(getOtherQueueName());
  }
  /**
   * 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) {
        }
      }
    }
  }
Beispiel #4
0
 public void tearDown() throws Exception {
   if (!isBroker08()) {
     _xaqueueConnection.close();
     _queueConnection.close();
   }
   super.tearDown();
 }
  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
 private void closeReceiver() {
   try {
     connection.close();
   } catch (JMSException e) {
     e.printStackTrace(System.err);
   }
 }
Beispiel #7
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);
    }
  }
Beispiel #8
0
 public void cerrarConexion() {
   try {
     conexionCola.stop();
     conexionCola.close();
   } catch (JMSException ex) {
     System.out.println("Excepcion");
   }
 }
 private void doCleanup() {
   if (queueConnection != null) {
     try {
       queueConnection.close();
     } catch (JMSException e) {
     }
   }
 }
Beispiel #10
0
 private void exit() {
   try {
     qConnect.close();
   } catch (JMSException jmse) {
     jmse.printStackTrace();
   }
   System.exit(0);
 }
 private void close(QueueConnection conn) {
   try {
     if (conn != null) {
       conn.close();
     }
   } catch (Exception e) {
   }
 }
  /**
   * 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) {
        }
      }
    }
  }
Beispiel #13
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();
      }
    }
  }
Beispiel #14
0
  protected synchronized void closeConnection() {
    try {
      if (null != m_sender) m_sender.close();
      if (null != m_session) m_session.close();
      if (null != m_connection) m_connection.close();
    } catch (Exception e) {
      getErrorHandler().error("Error closing connection", e, null);
    }

    m_sender = null;
    m_session = null;
    m_connection = null;
  }
 @Override
 public void tearDown() throws Exception {
   try {
     queueConnection.close();
     topicConnection.close();
   } catch (Exception ignored) {
   } finally {
     queueConnection = null;
     queueSession = null;
     topicConnection = null;
     topicSession = null;
     super.tearDown();
   }
 }
Beispiel #16
0
  @Test
  public void testCreateQueueConnection() throws Exception {

    server.getSecurityManager().addUser("IDo", "Exist");
    try {
      QueueConnection queueC =
          ((QueueConnectionFactory) cf).createQueueConnection("IDont", "Exist");
      fail("supposed to throw exception");
      queueC.close();
    } catch (JMSSecurityException e) {
      // expected
    }
    JMSContext ctx = cf.createContext("IDo", "Exist");
    ctx.close();
    ;
  }
  public void testQueueConnectionFactory() throws Exception {
    QueueConnectionFactory cf = null;
    QueueConnection c = null;

    try {
      cf = new QpidConnectionFactoryProxy();
      ((QpidConnectionFactoryProxy) cf).setConnectionURL(URL);
      c = cf.createQueueConnection();
      assertTrue(c instanceof QueueConnection);

    } finally {
      if (c != null) {
        c.close();
      }
    }
  }
Beispiel #18
0
      // Called by waitForCompletion
      public void close() {

        try {
          mp.close();
        } catch (Exception e) {
        }

        try {
          session.close();
        } catch (Exception e) {
        }

        try {
          qc.close();
        } catch (Exception 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();
    }
  }
Beispiel #20
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);
   }
 }
  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();
  }
 public void stop() throws JMSException {
   conn.stop();
   queueSession.close();
   conn.close();
 }
  public static void main(String[] args) throws Exception {
    InputStream is = TibcoSender.class.getClassLoader().getResourceAsStream("messages.properties");
    if (is == null) {
      System.out.println("message.properties must be in classpath");
      return;
    }
    Properties messages = new Properties();
    messages.load(is);
    is.close();
    QueueConnection queueCon = null;
    QueueSession queueSession = null;
    QueueSender sender = null;
    long idStart = 100003000l;
    Scanner in = new Scanner(System.in);
    System.out.print(
        "Enter amount of message you wish to send (set 0 to read amount from init.properties): ");
    int count = 0;
    try {
      count = in.nextInt();
    } catch (Exception e) {
      System.out.println("Error in reading input.");
    }
    Properties initial = new Properties();
    is = TibcoSender.class.getClassLoader().getResourceAsStream("init.properties");
    if (is != null) {
      initial.load(is);
      String value = initial.getProperty("start.id");
      if (value != null) idStart = Long.valueOf(value).longValue();
      value = initial.getProperty("count");
      if (value != null && count <= 0) count = Integer.valueOf(value).intValue();
    }

    if (args.length > 0) {
      try {
        idStart = Long.valueOf(args[0]);
        if (args.length > 1) count = Integer.valueOf(args[1]);
      } catch (Exception e) {
      }
    }
    initial.setProperty("count", "" + count);
    initial.setProperty("start.id", "" + (idStart + count));
    OutputStream os = null;
    if (is != null) {
      os =
          new FileOutputStream(
              new File(
                  TibcoSender.class.getClassLoader().getResource("init.properties").getPath()));
      is.close();
    } else {
      os = new FileOutputStream(new File("init.properties"));
    }
    initial.store(os, "");
    os.close();
    Properties config = new Properties();
    is = TibcoSender.class.getClassLoader().getResourceAsStream("config.properties");
    if (is != null) {
      config.load(is);
      is.close();
    }
    try {
      // messages

      // InitialContext properties
      Properties props = new Properties();
      props.setProperty(Context.INITIAL_CONTEXT_FACTORY, config.getProperty("JNDI_ICF"));
      props.setProperty(Context.PROVIDER_URL, config.getProperty("JNDI_URL"));

      // Jndi initialization
      InitialContext jndiContext = new InitialContext(props);

      // get the connection factory
      QueueConnectionFactory qcf =
          (QueueConnectionFactory) jndiContext.lookup(config.getProperty("JMS_QCF"));
      // and open a connection
      String user = config.getProperty("JMS_USER");
      String password = config.getProperty("JMS_PASSWORD");
      if (user != null && !user.isEmpty() && password != null && !password.isEmpty())
        queueCon = qcf.createQueueConnection(user, password);
      else queueCon = qcf.createQueueConnection();

      // Create a queue session using the connection
      queueSession = queueCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

      // get handle on queue, create a sender and send the message
      Queue topic = (Queue) jndiContext.lookup(config.getProperty("JMS_QUEUE"));
      sender = queueSession.createSender(topic);

      long i = 0; // new message index to be inserted into content of the message
      String[] cps = {
        "USD/HUF", "GBP/USD", "GBP/USD", "GBP/USD", "GBP/USD", "AUD/USD", "AUD/USD", "AUD/USD",
        "AUD/USD", "EUR/NOK", "USD/CZK", "EUR/USD", "EUR/JPY", "USD/HUF", "USD/SGD", "SGD/CHF"
      };
      String[] valuedates =
          new String
              [0]; // {"20110803", "20110810", "20110817", "20110824", "20110831", "20110913",
                   // "20110926"};
      String[] tenors = {"SPOT", "TN", "1W", "1W", "SPOT", "2W", "1M", "1W", "SPOT", "SPOT", "1W"};
      String[] clients = {
        "002-230", "003-0582", "004-0986", "Paypal1US", "003-0291"
      }; // FXB_OPERATINGCLIENT.APICLIENTID
      String tradeDate = date.format(new Date());

      for (int j = 0; j < count; j++) {
        i = idStart + j;
        String message =
            createMessage(
                idStart, i, j, cps, valuedates, tenors, clients, tradeDate, config, messages);

        // send message to topic at server.
        writeMessage(message, queueSession, sender, i);
      }

    } catch (Exception e) {
      System.out.println(e.getMessage());
      e.printStackTrace();
    } finally {
      // close sender, topic session and topic connection
      if (sender != null) {
        sender.close();
        sender = null;
      }
      if (queueSession != null) {
        queueSession.close();
        queueSession = null;
      }
      if (queueCon != null) {
        queueCon.close();
        queueCon = null;
      }
    }
  }
 /**
  * Closes JMS objects.
  *
  * @exception JMSException if JMS fails to close objects due to internal error
  */
 public void close() throws JMSException {
   qreceiver.close();
   qsession.close();
   qcon.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();
      }
    }
  }
 public void stop() throws JMSException {
   connection.stop();
   session.close();
   connection.close();
 }
  public void testCachingConnectionFactoryWithQueueConnectionFactoryAndJms102Usage()
      throws JMSException {
    MockControl cfControl = MockControl.createControl(QueueConnectionFactory.class);
    QueueConnectionFactory cf = (QueueConnectionFactory) cfControl.getMock();
    MockControl conControl = MockControl.createControl(QueueConnection.class);
    QueueConnection con = (QueueConnection) conControl.getMock();
    MockControl txSessionControl = MockControl.createControl(QueueSession.class);
    QueueSession txSession = (QueueSession) txSessionControl.getMock();
    MockControl nonTxSessionControl = MockControl.createControl(QueueSession.class);
    QueueSession nonTxSession = (QueueSession) nonTxSessionControl.getMock();

    cf.createQueueConnection();
    cfControl.setReturnValue(con, 1);
    con.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
    conControl.setReturnValue(txSession, 1);
    txSession.getTransacted();
    txSessionControl.setReturnValue(true, 1);
    txSession.rollback();
    txSessionControl.setVoidCallable(1);
    txSession.close();
    txSessionControl.setVoidCallable(1);
    con.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
    conControl.setReturnValue(nonTxSession, 1);
    nonTxSession.close();
    nonTxSessionControl.setVoidCallable(1);
    con.start();
    conControl.setVoidCallable(1);
    con.stop();
    conControl.setVoidCallable(1);
    con.close();
    conControl.setVoidCallable(1);

    cfControl.replay();
    conControl.replay();
    txSessionControl.replay();
    nonTxSessionControl.replay();

    CachingConnectionFactory scf = new CachingConnectionFactory(cf);
    scf.setReconnectOnException(false);
    Connection con1 = scf.createQueueConnection();
    Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session1.rollback();
    session1.close(); // should be ignored
    session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    session1.close(); // should be ignored
    con1.start();
    con1.close(); // should be ignored
    QueueConnection con2 = scf.createQueueConnection();
    Session session2 = con2.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
    session2.close(); // should be ignored
    session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
    session2.getTransacted();
    session2.close(); // should lead to rollback
    con2.start();
    con2.close(); // should be ignored
    scf.destroy(); // should trigger actual close

    cfControl.verify();
    conControl.verify();
    txSessionControl.verify();
    nonTxSessionControl.verify();
  }
 public void closeOrderSession(@Disposes @Order QueueConnection conn) throws JMSException {
   conn.close();
 }