JMS sample with glassfish

本文提供了一个使用 Java Message Service (JMS) 进行消息发送与接收的简单示例,通过创建连接工厂和目的地资源,实现消息的发送与监听接收。示例代码展示了如何配置 JMS 的环境变量、编译并运行 Java 类。
摘要由CSDN通过智能技术生成

1. cat send.java
package myjmssample;

import java.util.Properties;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class send {

  public static void main(String[] args) {
    String queueConnectionFactoryName = "JMSConnFactory";
    String queueName = "JMSDestResource";

    boolean transacted = false;
    int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;
    String message="Message need to send";

    Properties properties = new Properties();
    properties.put( Context.PROVIDER_URL, "iiop://localhost:3700" );
    properties.put( Context.INITIAL_CONTEXT_FACTORY,"com.sun.appserv.naming.S1ASCtxFactory" );


    try {
      Context context = new InitialContext(properties);
      Object obj = context.lookup(queueConnectionFactoryName);
      QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) obj;

      obj = context.lookup(queueName);
      Queue queue = (Queue) obj;

      QueueConnection queueConnection=queueConnectionFactory.createQueueConnection();
      queueConnection.start();
      QueueSession queueSession = queueConnection.createQueueSession(transacted, acknowledgementMode);
      TextMessage textMessage = queueSession.createTextMessage();
      textMessage.clearBody();
      textMessage.setText(message);

      QueueSender queueSender = queueSession.createSender(queue);
      queueSender.send(textMessage);

      if (transacted) {
        queueSession.commit();
      }

      if (queueSender != null) {
        queueSender.close();
      }

      if (queueSession != null) {
        queueSession.close();
      }

      if (queueConnection != null) {
        queueConnection.close();
      }

    }
    catch(Exception ex){
      ex.printStackTrace();
    }
  }
}

2. cat recv.java
package myjmssample;

import java.util.Properties;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class recv implements MessageListener {
  public void onMessage(Message message) {
    if (message instanceof TextMessage) {
      TextMessage textMessage = (TextMessage) message;
      try {
        System.out.println("Message content is:" + textMessage.getText());
      }
      catch (JMSException e) {
        e.printStackTrace();
      }
    }
  }

  public static void main(String[] args) {

    recv msgRcvr=new recv();
    String queueConnectionFactoryName = "JMSConnFactory";
    String queueName = "JMSDestResource";

    boolean transacted = false;
    int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;

    Properties properties = new Properties();
    properties.put( Context.PROVIDER_URL, "iiop://localhost:3700" );
    properties.put( Context.INITIAL_CONTEXT_FACTORY,"com.sun.appserv.naming.S1ASCtxFactory" );


    try {
      Context context = new InitialContext(properties);
      Object obj = context.lookup(queueConnectionFactoryName);
      QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory)
          obj;

      obj = context.lookup(queueName);
      Queue queue = (Queue) obj;

      QueueConnection queueConnection = queueConnectionFactory.
          createQueueConnection();
      queueConnection.start();
      QueueSession queueSession = queueConnection.createQueueSession(transacted,
          acknowledgementMode);
      QueueReceiver queueReceiver = queueSession.createReceiver(queue);

      queueReceiver.setMessageListener(msgRcvr);

      synchronized(msgRcvr){
        msgRcvr.wait(100000);
      }

      if (queueReceiver != null) {
        queueReceiver.close();
      }

      if (queueSession != null) {
        queueSession.close();
      }

      if (queueConnection != null) {
        queueConnection.close();
      }

    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

3. cat setClassPath.sh
#!/bin/ksh
export CLASSPATH=$CLASSPATH:/u/ainet/hui/openesb/glassfish-v2-ur2-b04-patch-20080616/lib/javaee.jar
export CLASSPATH=$CLASSPATH:/u/ainet/hui/openesb/glassfish-v2-ur2-b04-patch-20080616/lib/appserv-rt.jar
export CLASSPATH=$CLASSPATH:/u/ainet/hui/openesb/glassfish-v2-ur2-b04-patch-20080616/lib/appserv-admin.jar
export CLASSPATH=$CLASSPATH:/u/ainet/hui/openesb/glassfish-v2-ur2-b04-patch-20080616/lib/appserv-ext.jar
export CLASSPATH=$CLASSPATH:/u/ainet/hui/openesb/glassfish-v2-ur2-b04-patch-20080616/lib/install/applications/jmsra/imqjmsra.jar
export CLASSPATH=$CLASSPATH:./

4. compile
    . setClassPath
    javac myjmssample/send.java
    javac myjmssample/recv.java

5. run
    5.1 - Configure GlassFish JMS
       create Connection Factories = " JMSConnFactory"
       create Destination Resources = " JMSDestResource"
      
      

  5.2 run java myjmssample/send
        run java myjmssample/recv   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值