openjms的使用

使用的是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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值