关闭

openjms的使用

3245人阅读 评论(0) 收藏 举报

使用的是rmi,tcp,tcps方式。采用订阅模式。

下载openjms代码,然后把.jar包放到程序里面。

//receiver.java接收

import java.util.*;
import javax.jms.*;
import javax.naming.*;

import com.util.ReadProperties;

public class receiver {
 private TopicSubscriber m_topicSubscriber;

 public receiver(String strtopic) {
  try {
   // 取得JNDI上下文和连接
   /*
    * Hashtable properties = new Hashtable();
    * properties.put(Context.INITIAL_CONTEXT_FACTORY,
    * "org.exolab.jms.jndi.InitialContextFactory");
    * properties.put(Context.PROVIDER_URL, "rmi://" +
    * ReadProperties.get("jms_serversIP") + ":" +
    * ReadProperties.get("jms_serversPort") + "/"); Context context =
    * new InitialContext(properties);
    */

   Hashtable properties = new Hashtable();
   properties.put(Context.INITIAL_CONTEXT_FACTORY,
     "org.exolab.jms.jndi.InitialContextFactory");
   properties.put(Context.PROVIDER_URL, "tcps://192.168.0.205:3036/");

   properties.put("org.exolab.jms.net.tcps.keyStore",
     "E://works//note_name//src//client.keystore");
   properties.put("org.exolab.jms.net.tcps.keyStorePassword",
     "openjms");
   properties.put("org.exolab.jms.net.tcps.trustStore",
    "C://jsdk2//jre//lib//security//cacerts");
   System.err.println("gggggggggggggggggggggggggggg" + properties);
   Context context = null;

     context = new InitialContext(properties);


   System.err.println("gggggggggggggggggggggggggggg");

   // 获得Topic工厂和Connection
   TopicConnectionFactory factory = (TopicConnectionFactory) context
     .lookup("JmsTopicConnectionFactory");
   TopicConnection topicConnection = factory.createTopicConnection();
   topicConnection.start();

   // 创建Topic的会话,用于接收信息
   TopicSession topicSession = topicConnection.createTopicSession(
     false, Session.AUTO_ACKNOWLEDGE);

   // lookup topic1
   Topic topic = (Topic) context.lookup(strtopic);
   // 创建Topic subscriber
   m_topicSubscriber = topicSession.createSubscriber(topic);

  } catch (NamingException e) {
   e.printStackTrace();
  } catch (JMSException e) {
   e.printStackTrace();
  }
 }

 public String Recv() {
  String ret = "";
  try {
   // 同步消息接收,使用receive方法,堵塞等待,直到接收消息

   TextMessage message = (TextMessage) m_topicSubscriber.receive();
   ret = message.getText();
   return ret;
  } catch (JMSException e) {
   e.printStackTrace();
  }
  return ret;
 }

 public static void main(String[] args) {
  receiver rec = new receiver("Data_longcode_Recv");
  String re = rec.Recv();
  System.err.println(re);
 }
}

 

//Sender.java发送

import java.util.*;

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

import org.exolab.jms.net.util.SSLProperties;
import org.exolab.jms.server.net.ConnectorCfgFactory;
import com.sp.oscache.OSCache;
import com.util.*;

public class Sender {

 private TopicPublisher m_topicPublisher;

 private TopicSession m_topicSession;

 private int binit = 0;

 public Sender(String strtopic) {
  try {
   // 取得JNDI上下文和连接
  /* Hashtable properties = new Hashtable();
   properties.put(Context.INITIAL_CONTEXT_FACTORY,
     "org.exolab.jms.jndi.InitialContextFactory");
   // openJms默认的端口是1099
   // properties.put(Context.PROVIDER_URL,
   // "rmi://192.168.0.117:1099/");
   System.err.println(ReadProperties.get("jms_serversIP"));
   properties.put(Context.PROVIDER_URL, "rmi://"
     + ReadProperties.get("jms_serversIP") + ":"
     + ReadProperties.get("jms_serversPort") + "/");
*/
   Hashtable properties = new Hashtable();
   properties.put(Context.INITIAL_CONTEXT_FACTORY,
     "org.exolab.jms.jndi.InitialContextFactory");
   properties.put(Context.PROVIDER_URL, "https://192.168.0.206:3035");

   properties.put("org.exolab.jms.net.https.keyStore",
     "E://works//note_name//src//client.keystore");
   properties.put("org.exolab.jms.net.https.keyStorePassword",
     "openjms");
   properties.put("org.exolab.jms.net.https.trustStore",
    "C://jsdk2//jre//lib//security//cacerts");
   System.err.println("gggggggggggggggggggggggggggg" + properties);
   Context context = new InitialContext(properties);
   System.err.println("gggggggggggggggggggggggggggg");

   // 获得JMS Topic连接队列工厂
   TopicConnectionFactory factory = (TopicConnectionFactory) context
     .lookup("JmsTopicConnectionFactory");

   // 创建一个Topic连接,并启动

   TopicConnection topicConnection = factory.createTopicConnection();
   topicConnection.start();

   // 创建一个Topic会话,并设置自动应答
   m_topicSession = topicConnection.createTopicSession(false,
     Session.AUTO_ACKNOWLEDGE);

   // lookup 得到 topic1
   Topic topic = (Topic) context.lookup(strtopic);
   // 用Topic会话生成Topic发布器
   m_topicPublisher = m_topicSession.createPublisher(topic);
   binit = 1;
  } catch (NamingException e) {
   e.printStackTrace();
  } catch (JMSException e) {
   e.printStackTrace();
  }
 }

 public void Send(String strmsg) {
  if (binit == 0)
   System.out.println("请先初始化jms");
  try {

   TextMessage message = m_topicSession.createTextMessage(strmsg);
   m_topicPublisher.publish(message);
  } catch (JMSException e) {
   e.printStackTrace();
  }
 }

 public static void main(String[] args) {
  Sender send = new Sender("Data_longcode_Recv");
  send.Send("hello");
 }
}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:42119次
    • 积分:668
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    最新评论