JMS服务器和客户端程序实例

配置文件:jboss/server/default/deploy/jms/jbossmq-destinations-service.xml

配置
    <mbean code="org.jboss.mq.server.jmx.Topic"
  name="jboss.mq.destination:service=Topic,name=logInAndOutTopic">
    <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
    <depends optional-attribute-name="SecurityManager">jboss.mq:service=SecurityManager</depends>
    <attribute name="SecurityConf">
       <security>
         <role name="guest" read="true" write="true"/>
         <role name="publisher" read="true" write="true" create="false"/>
         <role name="durpublisher" read="true" write="true" create="true"/>
      </security>
    </attribute>
  </mbean>

服务器端
package test.jms;

import javax.jms.*;
import javax.naming.*;

public class LoginAndOutMessageSender {
  private static LoginAndOutMessageSender instance = new LoginAndOutMessageSender();
  private  TopicSession tsession = null;
  private  Topic topic = null;
  private  TopicPublisher tpub = null;

  private TopicConnectionFactory tcf = null;
  private TopicConnection tconn = null;
  private Context ctx = null;

  private LoginAndOutMessageSender() {
    init();
  }

  public static LoginAndOutMessageSender getInstance(){
    return instance;
  }


  public void sendLoginAndOutMessage(LoginAndOutMessageInfo msgInfo){
    int tryTimes = 0;
      while(true){
        try {
          if(msgInfo == null){
            break;
          }
          Message msg = tsession.createMessage();
          msg.setStringProperty("MSG_TYPE", msgInfo.getMsgType());
          msg.setStringProperty("USERID", msgInfo.getUserId());
          msg.setStringProperty("USERIP", msgInfo.getUserIp());
          msg.setStringProperty("USERMAC", msgInfo.getUserMac());
          msg.setStringProperty("NASIP", msgInfo.getNasIp());
          msg.setIntProperty("NASPORT", msgInfo.getNasPort());

          tpub.publish(msg);
          break;
        }
        catch (Exception e) {
          if (!init()) {
            if (tryTimes < 20) { //默认15分钟可以恢复数据库连接,这里冗余一部分
              tryTimes++;
              try{
                Thread.sleep(60000); //一分种后重试
              }
              catch(Exception ex){
                Logger.log(Logger.DEBUG_TYPE,ex);
              }
              continue;
            }
            else{
              System.out.println("系统消息机制异常,系统将自动退出!请进行系统恢复!");
              SysTool.exit(0);
              return;
            }
          }
        }
      }


  }

  private boolean init() {
    try {
      try{
        tpub.close();
      }
      catch(Exception ex){ }
      try{
        tsession.close();
      }
      catch(Exception ex){  }
      try{
        tconn.close();
      }
      catch(Exception ex){  }
      try{
        ctx.close();
      }
      catch(Exception ex){ }

      ctx = new InitialContext();
      tcf = (TopicConnectionFactory)ctx.lookup(
        "ConnectionFactory");
      tconn = tcf.createTopicConnection();
      tsession = tconn.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
      topic = (Topic)ctx.lookup("topic/logInAndOutTopic");
      tpub = tsession.createPublisher(topic);
      tconn.start();
      return true;
    }
    catch (Exception e) {
      Logger.log(Logger.DEBUG_TYPE, e);
      return false;
    }
  }

}

客户端程序
package test;
import java.util.Hashtable;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;

public class JMSClinet {
  public static void main(String[] args) {

        while(true){
        try {
            TopicSession tsession = null;

            TopicSubscriber tsub = null;
            Hashtable ht = new Hashtable();
            ht.put(Context.INITIAL_CONTEXT_FACTORY,
                    "org.jnp.interfaces.NamingContextFactory");
            ht.put(Context.PROVIDER_URL, "192.168.0.203:1099");
            ht.put("java.naming.rmi.security.manager", "yes");
            ht.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");

            Context ctx = new InitialContext(ht);

            TopicConnectionFactory factory = (TopicConnectionFactory) ctx
                    .lookup("ConnectionFactory");

            TopicConnection connection = factory.createTopicConnection();


            TopicSession session = connection.createTopicSession(false,
                    Session.AUTO_ACKNOWLEDGE);

            Topic topic = (Topic) ctx.lookup("topic/logInAndOutTopic");

            tsub = session.createSubscriber(topic);

            connection.start();
            Message msg = tsub.receive();
            String msgType = msg.getStringProperty("MSG_TYPE");
            String userId = msg.getStringProperty("USERID");
            String userIp = msg.getStringProperty("USERIP");
            String userMac = msg.getStringProperty("USERMAC");
            String nasIp = msg.getStringProperty("NASIP");
            int nasPort = msg.getIntProperty("NASPORT");
   
            System.out.println("msgType is " + msgType);
            System.out.println("userId is " + userId);
            System.out.println("userIp is " + userIp);
            System.out.println("userMac is " + userMac);
            System.out.println("nasIp is " + nasIp);
            System.out.println("nasPort is " + nasPort);
           
            connection.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值