`

weblogic jms

    博客分类:
  • JMS
 
阅读更多

package jms;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class QueueSend {

 // Defines the JNDI context factory.
 public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";

 // Defines the JNDI provider url.
 public final static String PROVIDER_URL = "t3://localhost:7001";

 // Defines the JMS connection factory for the queue.
 public final static String JMS_FACTORY = "myjmsconnectionfactory";

 // Defines the queue 用的是对应 QUEUE的JNDI名子
 public final static String QUEUE = "myjmsqueue";

 private QueueConnectionFactory qconFactory;

 private QueueConnection qcon;

 private QueueSession qsession;

 private QueueSender qsender;

 private Queue queue;

 private TextMessage msg;

 private StreamMessage sm;

 private BytesMessage bm;

 private MapMessage mm;

 private ObjectMessage om;

 /**
  * Creates all the necessary objects for sending messages to a JMS queue.
  *
  * @param ctx
  *            JNDI initial context
  * @param queueName
  *            name of queue
  * @exception NamingException
  *                if operation cannot be performed
  * @exception JMSException
  *                if JMS fails to initialize due to internal error
  */
 public void init(Context ctx, String queueName) throws NamingException, JMSException {
  qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
  qcon = qconFactory.createQueueConnection();
  qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
  queue = (Queue) ctx.lookup(queueName);
  qsender = qsession.createSender(queue);

  msg = qsession.createTextMessage();
  sm = qsession.createStreamMessage();
  bm = qsession.createBytesMessage();
  mm = qsession.createMapMessage();
  om = qsession.createObjectMessage();

  qcon.start();
 }

 /**
  * Sends a message to a JMS queue.
  *
  * @param message
  *            message to be sent
  * @exception JMSException
  *                if JMS fails to send message due to internal error
  */
 public void send(String message) throws JMSException {
  // set TextMessage
  msg.setText(message);

  // set StreamMessage
  sm.writeString("xmddl369");
  sm.writeDouble(23.33);

  // set BytesMessage
  String name = "xmddl369";
  byte[] block = name.getBytes();
  bm.writeBytes(block);

  // set MapMessage
  mm.setString("name", "xmddl369");

  // set ObjectMessage
  UserInfo ui = new UserInfo();
  ui.setName("xmddl369");
  ui.setAddress("厦门");
  ui.setAge(100);
  om.setObject(ui);

  qsender.send(msg);
  qsender.send(sm);
  qsender.send(bm);
  qsender.send(mm);
  qsender.send(om);
 }

 /**
  * Closes JMS objects.
  *
  * @exception JMSException
  *                if JMS fails to close objects due to internal error
  */
 public void close() throws JMSException {
  qsender.close();
  qsession.close();
  qcon.close();
 }

 public static void main(String[] args) throws Exception {
  InitialContext ic = getInitialContext();
  QueueSend qs = new QueueSend();
  qs.init(ic, QUEUE);
  //readAndSend(qs);
  qs.send("ddd");
  qs.close();
 }

 private static void readAndSend(QueueSend qs) throws IOException, JMSException {
  BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
  String line = null;
  boolean quitNow = false;
  do {
   System.out.print("Enter message (\"quit\" to quit): ");
   line = msgStream.readLine();
   if (line != null && line.trim().length() != 0) {
    qs.send(line);
    System.out.println("JMS Message Sent: " + line + "\n");
    quitNow = line.equalsIgnoreCase("quit");
   }
  } while (!quitNow);

 }

 private static InitialContext getInitialContext() throws NamingException {
  Hashtable env = new Hashtable();
  env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
  env.put(Context.PROVIDER_URL, PROVIDER_URL);
  return new InitialContext(env);
 }
}

 

package jms;

import java.util.Hashtable;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Administrator
 *
 *         <pre>
 *      修改版本:  修改人:  修改日期:  修改内容:
 * </pre>
 */
public class QueueReceive implements MessageListener {
 // Defines the JNDI context factory.
 public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";

 // Defines the JNDI provider url.
 public final static String PROVIDER_URL = "t3://localhost:7001";

 // Defines the JMS connection factory for the queue.
 public final static String JMS_FACTORY = "myjmsconnectionfactory";

 // Defines the queue 用的是对应 QUEUE的JNDI名子
 public final static String QUEUE = "myjmsqueue";

 private QueueConnectionFactory qconFactory;

 private QueueConnection qcon;

 private QueueSession qsession;

 private QueueReceiver qreceiver;

 private Queue queue;

 private boolean quit = false;

 /**
  * Message listener interface.
  *
  * @param msg
  *            message
  */
 public void onMessage(Message msg) {
  try {
   String msgText = "";
   double d = 0;

   if (msg instanceof TextMessage) {
    msgText = ((TextMessage) msg).getText();
   } else if (msg instanceof StreamMessage) {
    msgText = ((StreamMessage) msg).readString();
    d = ((StreamMessage) msg).readDouble();
   } else if (msg instanceof BytesMessage) {
    byte[] block = new byte[1024];
    ((BytesMessage) msg).readBytes(block);
    msgText = String.valueOf(block);
   } else if (msg instanceof MapMessage) {
    msgText = ((MapMessage) msg).getString("name");
   } else if (msg instanceof ObjectMessage) {
    UserInfo ui = (UserInfo) ((ObjectMessage) msg).getObject();
    msgText = ui.getName();
    d = ui.getAge();
   }

   System.out.println("Message Received: " + msgText + "\t" + d);

   if (msgText.equalsIgnoreCase("quit")) {
    synchronized (this) {
     quit = true;
     this.notifyAll(); // Notify main thread to quit
    }
   }
  } catch (JMSException jmse) {
   jmse.printStackTrace();
  }
 }

 /**
  * Creates all the necessary objects for receiving messages from a JMS
  * queue.
  *
  * @param ctx
  *            JNDI initial context
  * @param queueName
  *            name of queue
  * @exception NamingException
  *                if operation cannot be performed
  * @exception JMSException
  *                if JMS fails to initialize due to internal error
  */
 public void init(Context ctx, String queueName) throws NamingException, JMSException {
  qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
  qcon = qconFactory.createQueueConnection();
  qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
  queue = (Queue) ctx.lookup(queueName);
  qreceiver = qsession.createReceiver(queue);
  qreceiver.setMessageListener(this);
  qcon.start();
 }

 /**
  * Closes JMS objects.
  *
  * @exception JMSException
  *                if JMS fails to close objects due to internal error
  */
 public void close() throws JMSException {
  qreceiver.close();
  qsession.close();
  qcon.close();
 }

 /**
  * main() method.
  *
  * @param args
  *            WebLogic Server URL
  * @exception Exception
  *                if execution fails
  */

 public static void main(String[] args) throws Exception {

  InitialContext ic = getInitialContext();
  QueueReceive qr = new QueueReceive();
  qr.init(ic, QUEUE);

  System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");

  // Wait until a "quit" message has been received.
  synchronized (qr) {
   while (!qr.quit) {
    try {
     qr.wait();
    } catch (InterruptedException ie) {
     ie.printStackTrace();
    }
   }
  }
  qr.close();
 }

 private static InitialContext getInitialContext() throws NamingException {
  Hashtable env = new Hashtable();
  env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
  env.put(Context.PROVIDER_URL, PROVIDER_URL);
  return new InitialContext(env);
 }

 public String getEncoding() throws Exception {
  return "Hello World!";
 }
}

 

 

原文链接:http://wangpx.iteye.com/blog/1040411

 

 

分享到:
评论

相关推荐

    Weblogic JMS 依赖jar包下载

    Weblogic JMS 依赖jar包是Oracle公司的中间件产品Weblogic Server中用于实现Java Message Service (JMS) 功能的关键组件。JMS是一种标准API,它允许应用程序在分布式环境中发送和接收消息,提供了异步通信的能力。在...

    Spring+Weblogic JMS

    WebLogic JMS是Oracle WebLogic Server提供的消息中间件,它遵循JMS规范,用于在分布式环境中传递消息,实现应用程序间的异步通信。WebLogic JMS提供了消息生产者、消费者、队列和主题等概念,确保消息的可靠传输。 ...

    java weblogic jms temple

    自己写好的一个简单的weblogic jms的简单实现,包括点对点的实现,以及topic的实现,适合初学者参考

    Programming WebLogic JMS

    ### 编程WebLogic JMS:深入理解与实践 #### 一、引言 随着企业级应用对消息处理的需求日益增长,Java消息服务(JMS)作为一种标准的消息中间件技术,已经成为开发分布式应用的重要组成部分。BEA WebLogic Server...

    WebLogic jms三个实例

    实现了jms的queue和topic,同时也实现了远程传送消息和本地传送消息,另外也分别实现了session的事务和非事务模式,有了这三个例子应该可以满足你的任何关于jms的需求了,由于上传限制,另外还需要下载有个WebLogic包...

    weblogic_jms服务器配置

    WebLogic Server 是一款由 Oracle 公司提供的企业级 Java 应用服务器,它支持 Java Message Service (JMS),一个标准的 Java API,用于在分布式环境中发送和接收消息。WebLogic JMS 服务器是 WebLogic Server 的一...

    Linux环境Weblogic集群配置,Weblogic JMS集群配置

    本教程将深入探讨如何在Linux环境下配置Weblogic集群以及Weblogic JMS集群,这对于提高应用程序的可用性和可扩展性至关重要。 首先,让我们了解一下Weblogic集群的基本概念。Weblogic集群是指一组独立的Weblogic...

    springboot集成weblogic的jms源码.zip

    这个"springboot集成weblogic的jms源码.zip"压缩包提供了将Spring Boot应用与WebLogic的JMS服务集成的示例代码。 首先,我们来看`JmsConfiguration.java`文件。这是一个配置类,通常用于设置与JMS相关的bean。在...

    spring jms 整合 weblogic jms

    本人开发的spring jms项目,已经上线近一年了,这里是我项目jms配置文件,使用的是spring jms 整合weblogic jms。如果真的需要,请咨询我,并且附上我上传的这个配置文件,附近中没有带有这个文件,一律不作任何回答...

    spring,weblogic配置jms

    Spring框架是一个广泛使用的Java应用开发框架,它提供了与多种消息中间件集成的能力,包括WebLogic Server的JMS服务。WebLogic是Oracle公司的一款企业级应用服务器,它支持JMS规范,提供了强大的消息队列和发布/订阅...

    Spring整合Weblogic jms实例详解

    Spring 整合 Weblogic JMS 实例详解 Spring 整合 Weblogic JMS 实例详解是指在 Spring 框架下整合 Weblogic JMS 服务器,以便在应用程序中使用 JMS messaging。这种整合方式可以提供高性能、可靠性和灵活性的消息...

    Weblogic_JMS+配置数据源配置

    WebLogic Server是一款由Oracle公司提供的企业级Java应用服务器,它提供了包括JMS(Java Message Service)在内的多种中间件服务。JMS是Java平台上的标准消息传递API,用于在分布式环境中发送和接收消息。配置...

    weblogic中使用JMS发送和接受消息

    WebLogic Server是一款由Oracle公司提供的企业级应用服务器,它支持Java Message Service (JMS) 规范,允许在分布式环境中可靠地发送和接收消息。JMS是Java平台上的标准接口,用于实现应用程序间的异步通信。本文将...

    Weblogic创建JMS服务器

    WebLogic Server是一款由Oracle公司提供的企业级Java应用服务器,它支持Java EE标准并提供各种服务,包括JMS(Java Message Service)服务器。JMS是一种在分布式环境中传递消息的标准API,它允许应用程序创建、发送...

    WebLogic_JMS讲解

    ### WebLogic_JMS详解 #### 一、WebLogic_JMS概览 WebLogic_JMS是Oracle WebLogic Server中集成的企业级消息中间件解决方案,它基于Java Message Service (JMS) API标准,为开发者提供了构建高性能、高可靠性的...

    weblogic jms c程序

    在WebLogic环境中,你可能需要确保JMS连接、会话和消费者都已正确配置和关闭,以确保优雅的退出。当收到SIGINT信号时,除了清理信号处理器之外,还应该确保所有JMS资源被适当关闭,防止数据丢失或资源泄漏。 为了更...

    JMS 简介以及Weblogic配置JMS图解

    例如,IBM的MQSeries、BEA的WebLogic JMS服务和Progress的SonicMQ都是JMS提供者。 2. **JMS客户**:基于Java的应用程序或对象,它们是消息的生产者或消费者。 3. **JMS生产者**:创建并发送消息的组件。 4. **JMS...

Global site tag (gtag.js) - Google Analytics