水稳碎石水泥剂量:WebLogic JMS实例:使用Spring开发JMS

来源:百度文库 编辑:偶看新闻 时间:2024/05/03 11:51:55

Spring的JMS框架抽象了JMS API的使用并与JMS提供者(如WebLogic Server,WebSphere MQ)。下面以一个简单的发送接收消息的例子来说明如何使用Spring Jms。
准备:下载WebLogic9.2安装,并配置相关Jms Server和Jms Module。

使用Eclipse建立java project,将相关weblogic包加入到工程。

建立配置文件
使用Spring IoC的方法建立相关Jms对象,下面是jmsconfig.xml具体内容。
jmsconfig.xml


http://www.springframework.org/dtd/spring-beans.dtd">


   class="org.springframework.jndi.JndiTemplate">
  
   
    
    
      t3://150.236.80.44:7001
    

    
      weblogic.jndi.WLInitialContextFactory
    

    
    
      weblogic
    

    
      weblogic
    

   

  



   class="org.springframework.jndi.JndiObjectFactoryBean">
  
    jms.connFactory
  

  
   
  



   class="org.springframework.jndi.JndiObjectFactoryBean">
  
   
  

  
    jms.queue
  


   class="org.springframework.jms.support.converter.SimpleMessageConverter" />

   class="org.springframework.jms.core.JmsTemplate">
  
   
  

  
   
  

  
   
  



   class="org.springframework.jndi.JndiObjectFactoryBean">
  
   
  

  
    jms.queue
  


   class="org.springframework.jms.core.JmsTemplate">
  
   
  

  
   
  

  
   
  



建立测试类:
发送消息类JmsSenderApp.java
package jms.sample.jmstemplate;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class JmsSenderApp {
public static void main(String[] args) {
   ApplicationContext ctx = new FileSystemXmlApplicationContext(
     "jmsconfig.xml");
   JmsTemplate jmsSender = (JmsTemplate) ctx.getBean("jmsTemplateSender");
   System.out.println("send message:");
   jmsSender.send(new MessageCreator() {
    public Message createMessage(Session session) throws JMSException {
     return session.createTextMessage("This is a sample message");
    }
   });
   System.out.println("ok, send message end");
}
}

接收消息类JmsReceiverApp.java
package jms.sample.jmstemplate;

import javax.jms.JMSException;
import javax.jms.Message;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

public class JmsReceiverApp {
public static void main(String[] args) throws JMSException {
   ApplicationContext ctx = new FileSystemXmlApplicationContext(
     "jmsconfig.xml");
   JmsTemplate jmsReceiver=(JmsTemplate)ctx.getBean("jmsTemplateReceiver");
   System.out.println("begin receiving message:");
   Message msg=jmsReceiver.receive();
   System.out.println("received messages: "+msg.toString()+msg.getJMSDestination());
   System.out.println("receive message end");
}
}
先执行JmsSenderApp发送消息,然后执行JmsSenderApp接收消