示例#1
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);
    }
  }
示例#2
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) {
        }
      }
    }
  }
  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();
    }
  }