Weblogic-JMS_PUB_SUB

package com.umessage.smscenter.sms.test;

import javax.jms.MessageListener;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.TopicSession;
import javax.jms.TopicPublisher;
import javax.jms.TopicSubscriber;
import javax.jms.Topic;
import java.util.Properties;
import java.io.Serializable;
import javax.jms.Message;


/**
 <p>Title:JMS Demo</p>
 <p>Description:JMS PTP方式发布和接受消息</p>
 <p>Copyright: Copyright (c) 2007</p>
 <p>Company: Umessage</p>
 <p>@author xiaobian</p>
 <p>@version 1.0</p>

 * Use this class to publish and subscribe messages.
 *
 * To send a text message:
 * <code>
 * JMSpublicTest jMSpublicTest = new JMSpublicTest();
 * jMSpublicTest.publishText("Hello world");
 * jMSpublicTest.close(); //Release resources
 * </code>
 * 
 * To receive a message:
 * <code>0
 * JMSpublicTest jMSpublicTest = new JMSpublicTest();
 * jMSpublicTest.getTopicSubscriber();
 * </code>
 */

public class JMSpublicTest implements MessageListener {
  //定义上下文环境
  private static Context context;
  //定义事务
  private boolean transacted = false;
  //定义接收消息的模式
  private int acknowledgementMode = javax.jms.Session.AUTO_ACKNOWLEDGE;
  //定义主题连接工厂
  private TopicConnectionFactory topicConnectionFactory;
  //定义主题连接
  private TopicConnection topicConnection;
  //定义主题会话 
  private TopicSession topicSession;
  //定义主题发布者
  private TopicPublisher topicPublisher;
  //定义主题的接收者
  private TopicSubscriber topicSubscriber;
  //定义主题消息 
  private Topic topic;
  
  //定义主题连接工厂名称(服务器中配置的JNDI名称) 
  private String topicConnectionFactoryName = "smsConnectionFactory";
  //定义主题发布者(JNDI名称)
  private String publishTopicName = "SendTopic";
  //定义主题接收者(JNDI名称)
  private String subscribeTopicName = "ReceiveTopic";
  //客户端ID ?
  private String clientId = "";
  //定义持久名称
  private String durableName = "";
  //是否持久化
  private boolean durable = false;
  
  public void setTransacted(boolean transacted) {
    this.transacted = transacted;
  }

  //获得主题接受者并打开连接
  public TopicSubscriber getTopicSubscriber() throws Exception {
    if (topicSubscriber == null) {
      if (isDurable()) {
        topicSubscriber = getTopicSession(true).createDurableSubscriber(getSubscribeTopic(), getDurableName());
      }
      else {
        topicSubscriber = getTopicSession(true).createSubscriber(getSubscribeTopic());
      }
      topicSubscriber.setMessageListener(this);
      getTopicConnection(true).start();
    }
    return topicSubscriber;
  }
  //根据主题的JNDI名称获得订阅主题
  public Topic getSubscribeTopic() throws Exception {
	    if (topic == null) {
	      Object obj = getContext().lookup(subscribeTopicName);
	      topic = (Topic) obj;
	    }
	    return topic;
	  }
  //获得主题的发布者
  public TopicPublisher getTopicPublisher() throws Exception {
    if (topicPublisher == null) {
      topicPublisher = getTopicSession(false).createPublisher(getPublishTopic());
    }
    return topicPublisher;
  }
  //根据主题的JNDI名称获得发布主题
  public Topic getPublishTopic() throws Exception {
    if (topic == null) {
      Object obj = getContext().lookup(publishTopicName);
      topic = (Topic) obj;
    }
    return topic;
  }
  //获得主题会话
  public TopicSession getTopicSession(boolean consumer) throws Exception {
    if (topicSession == null) {
      topicSession = getTopicConnection(consumer).createTopicSession(isTransacted(), getAcknowledgementMode());
    }
    return topicSession;
  }
  //获得主题会话 
  public TopicConnection getTopicConnection(boolean consumer) throws Exception {
    if (topicConnection == null) {
      topicConnection = getTopicConnectionFactory().createTopicConnection();
      if (isDurable() && consumer) {
        topicConnection.setClientID(clientId);
      }
      topicConnection.start();
    }
    return topicConnection;
  }
  //获得主题的连接工厂
  public TopicConnectionFactory getTopicConnectionFactory() throws Exception {
    if (topicConnectionFactory == null) {
      Object obj = getContext().lookup(topicConnectionFactoryName);
      System.out.println("topicConnectionFactory=="+obj);
      topicConnectionFactory = (TopicConnectionFactory) obj;
    }
    return topicConnectionFactory;
  }

  public void setDurable(boolean durable) {
    this.durable = durable;
  }

  public boolean isDurable() {
    return durable;
  }

  public void setDurableName(String durableName) {
    this.durableName = durableName;
  }

  public String getDurableName() {
    return durableName;
  }

  public void setClientId(String clientId) {
    this.clientId = clientId;
  }

  public String getClientId() {
    return clientId;
  }

  public void setSubscribeTopicName(String subscribeTopicName) {
    this.subscribeTopicName = subscribeTopicName;
  }

  public String getSubscribeTopicName() {
    return subscribeTopicName;
  }

  public void setPublishTopicName(String publishTopicName) {
    this.publishTopicName = publishTopicName;
  }

  public String getPublishTopicName() {
    return publishTopicName;
  }

  public void setTopicConnectionFactoryName(String topicConnectionFactoryName) {
    this.topicConnectionFactoryName = topicConnectionFactoryName;
  }

  public String getTopicConnectionFactoryName() {
    return topicConnectionFactoryName;
  }

  public void setAcknowledgementMode(int acknowledgementMode) {
    this.acknowledgementMode = acknowledgementMode;
  }

  public int getAcknowledgementMode() {
    return acknowledgementMode;
  }

  public boolean isTransacted() {
    return transacted;
  }
  //获得应用服务器上下文
  public Context getInitialContext() throws Exception {
    
	String url = "t3://127.0.0.1:7001";
    String user = "xiaobian";
    String password = "12345678";

    Properties properties;
    try {
      properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
      properties.put(Context.PROVIDER_URL, url);
      if (user != null) {
        properties.put(Context.SECURITY_PRINCIPAL, user);
        properties.put(Context.SECURITY_CREDENTIALS,password == null ? "" : password);
      }
      return new javax.naming.InitialContext(properties);
    }
    catch (Exception e) {
      System.out.println("Unable to connect to WebLogic server at " + url);
      System.out.println("Please make sure that the server is running.");
      throw e;
    }
  }
  
  //返回应用服务器上下文
  private Context getContext() throws Exception {
    if (context == null) {
      try {
        context = getInitialContext();
      }
      catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
      }
    }
    return context;
  }

  //发布文本消息
  public void publishText(String message) throws Exception {
    javax.jms.TextMessage textMessage = getTopicSession(false).createTextMessage();
    textMessage.clearBody();
    textMessage.setText(message);
    getTopicPublisher().publish(textMessage);
    if (isTransacted()) {
      getTopicSession(false).commit();
    }
  }
  //发布对象消息	
  public void publishObject(Serializable message) throws Exception {
    javax.jms.ObjectMessage objectMessage = getTopicSession(false).createObjectMessage();
    objectMessage.clearBody();
    objectMessage.setObject(message);
    getTopicPublisher().publish(objectMessage);
    if (isTransacted()) {
      getTopicSession(false).commit();
    }
  }
  
  //定义消息驱动Bean. 可以写在一个文件中 . 实现监听器类
  public void onMessage(Message message) {
    if (message instanceof javax.jms.BytesMessage) {
      javax.jms.BytesMessage bytesMessage = (javax.jms.BytesMessage) message;
      System.out.println("这是一个BytesMessage,内容是:" + bytesMessage);
      /** @todo Process bytesMessage here */
    }
    else if (message instanceof javax.jms.MapMessage) {
      javax.jms.MapMessage mapMessage = (javax.jms.MapMessage) message;
      System.out.println("这是一个MapMessage,内容是:" + mapMessage);
      /** @todo Process mapMessage here */
    }
    else if (message instanceof javax.jms.ObjectMessage) {
      javax.jms.ObjectMessage objectMessage = (javax.jms.ObjectMessage) message;
      System.out.println("这是一个objectMessage,内容是:" + objectMessage);
      /** @todo Process objectMessage here */
    }
    else if (message instanceof javax.jms.StreamMessage) {
      javax.jms.StreamMessage streamMessage = (javax.jms.StreamMessage) message;
      System.out.println("这是一个StreamMessage,内容是:" + streamMessage);
      /** @todo Process streamMessage here */
    }
    else if (message instanceof javax.jms.TextMessage) {
      javax.jms.TextMessage objectMessage = (javax.jms.TextMessage) message;
      System.out.println("这是一个TextMessage,内容是:" + objectMessage);
      /** @todo Process textMessage here */
    }
    if (isTransacted()) {
      try {
        getTopicSession(false).commit();
      }
      catch (Exception ex) {
        ex.printStackTrace();
      }
    }
  }
  //释放资源
  public void close() throws Exception {
    if (topicPublisher != null) {
      topicPublisher.close();
    }
    if (topicSubscriber != null) {
      topicSubscriber.close();
    }
    if (topicSession != null) {
      topicSession.close();
    }
    if (topicConnection != null) {
      topicConnection.close();
    }
    System.out.println("Release Resource");
  }
  /******************************************************************************************************************/
  //	测试 
  /******************************************************************************************************************/
  public static void main(String[] args) throws InterruptedException, Exception {

	    JMSpublicTest jMsPublic = new JMSpublicTest();
	    jMsPublic.setDurable(true);
	    jMsPublic.setClientId("IP:192.168.0.102");
	    jMsPublic.setDurableName("test226aa");
	    TopicSubscriber topicSub = jMsPublic.getTopicSubscriber();
	   
	    //开始发布一个消息
	    JMSpublicTest jtp = new JMSpublicTest();
	    jtp.setDurable(true);
	    jtp.setClientId("ip:192.168.0.114");
	    jtp.setDurableName("test226aa");
	    

	    jtp.publishText("发布一个消息!!!");
	    jtp.publishText("发布一个消息!!!");

	//    System.out.println("Topic Name=="+topicSub.getTopic().getTopicName());
		
	    jtp.close();
	    //在这里保持监听来获取消息
	    synchronized (jMsPublic) {
	    	jMsPublic.wait(10000);
	    }
	    jMsPublic.close();
	   // System.out.println(jtp.getClientId());
	    System.out.println("接收消息");
	    

	  }

}


你可能感兴趣的:(应用服务器,bean,weblogic,jms,Security)