openjms的使用

原创 2006年06月07日 15:39:00

使用的是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");
 }
}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

openjms 开源jms

  • 2014-07-11 15:45
  • 7.11MB
  • 下载

OPENJMS操作消息传输示例

  • 2009-11-29 21:21
  • 15KB
  • 下载

java开源消息服务(OpenJMS&ActiveMQ)

最近公司的一个中间件框架中要提供消息服务。我考虑了下,对消息服务的要求最主要的就是: 开源,能没有顾忌的的嵌套到公司的框架中 嵌套方便,开源的项目虽然可以修改,但是如果需要改动非常复杂,需要改动大...

OpenJMS简介

  • 2007-12-17 14:40
  • 56KB
  • 下载

openjms-0.7.7-beta-1.zip

  • 2012-03-01 14:21
  • 7.11MB
  • 下载

JMS服务器openJms入门

本文介绍开源的JMS服务器openJms,及怎样使用openJms来构建系统之间健全、高度可用的通讯,从而简化企业级应用的开发。 openJms符合SUN的JMS API 1.0.2规范,支持消息队列...

openjms-0.7.7-beta-1-src

  • 2008-08-31 21:34
  • 1.52MB
  • 下载

JMS OPENJMS的实现例子

  • 2009-02-02 16:05
  • 10KB
  • 下载

java开源消息服务(OpenJMS&ActiveMQ)-OpenJMS学习笔记二(运行)

运行OpenJMS 我们先看下官方文档。 Starting OpenJMS To start the OpenJMS server, open a command prompt an...

Jms插件openJMs

  • 2012-11-28 17:17
  • 7.11MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)