1 Reply Latest reply on Feb 18, 2004 5:33 AM by Adrian Brock

    HOW-TO config JBoss for This JMS sample?

    wangid Newbie

      as titile...

      i got a sample about JMS, the code is below:
      =====
      import java.io.*;
      import javax.jms.*;
      import javax.naming.*;

      public class QSender {

      public static void main(String[] args) {

      new QSender().send();
      }

      public void send() {

      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

      try {
      //Prompt for JNDI names
      System.out.println("Enter QueueConnectionFactory name:");
      String factoryName = reader.readLine();
      System.out.println("Enter Queue name:");
      String queueName = reader.readLine();

      //Look up administered objects
      InitialContext initContext = new InitialContext();
      QueueConnectionFactory factory =
      (QueueConnectionFactory) initContext.lookup(factoryName);
      Queue queue = (Queue) initContext.lookup(queueName);
      initContext.close();

      //Create JMS objects
      QueueConnection connection = factory.createQueueConnection();
      QueueSession session =
      connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      QueueSender sender = session.createSender(queue);

      //Send messages
      String messageText = null;
      while (true) {
      System.out.println("Enter message to send or 'quit':");
      messageText = reader.readLine();
      if ("quit".equals(messageText))
      break;
      TextMessage message = session.createTextMessage(messageText);
      sender.send(message);
      }

      //Exit
      System.out.println("Exiting...");
      reader.close();
      connection.close();
      System.out.println("Goodbye!");

      } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
      }
      }
      }
      =====
      import java.io.*;
      import javax.jms.*;
      import javax.naming.*;

      public class QReceiver implements MessageListener {

      private boolean stop = false;

      public static void main(String[] args) {

      new QReceiver().receive();
      }

      public void receive() {

      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

      try {
      //Prompt for JNDI names
      System.out.println("Enter QueueConnectionFactory name:");
      String factoryName = reader.readLine();
      System.out.println("Enter Queue name:");
      String queueName = reader.readLine();
      reader.close();

      //Look up administered objects
      InitialContext initContext = new InitialContext();
      QueueConnectionFactory factory =
      (QueueConnectionFactory) initContext.lookup(factoryName);
      Queue queue = (Queue) initContext.lookup(queueName);
      initContext.close();

      //Create JMS objects
      QueueConnection connection = factory.createQueueConnection();
      QueueSession session =
      connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      QueueReceiver receiver = session.createReceiver(queue);
      receiver.setMessageListener(this);
      connection.start();

      //Wait for stop
      while (!stop) {
      Thread.sleep(1000);
      }

      //Exit
      System.out.println("Exiting...");
      connection.close();
      System.out.println("Goodbye!");

      } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
      }
      }

      public void onMessage(Message message) {

      try {
      String msgText = ((TextMessage) message).getText();
      System.out.println(msgText);
      if ("stop".equals(msgText))
      stop = true;
      } catch (JMSException e) {
      e.printStackTrace();
      stop = true;
      }
      }
      }
      =====
      and how can i test it in Jboss? i am very beginner, someone help me...