---
title: "基于jms使用ActiveMQ实现异步日志功能.消息持久到oracle 10g 数据库"
author: "loving863"
date: "2008-09-18T15:05:51Z"
tags: ["ActiveMQ", "Apache", "JMS", "Oracle", "SQL Server"]
categories: ["企业架构"]
slug: "jms-activemq-oracle-10g"
draft: false
lastmod: "2008-09-18T15:05:51Z"
id: 243550
url: "https://jinguo.iteye.com/blog/243550"
---

package askyaya.entity;  
import java.io.Serializable;  
import org.apache.commons.lang.builder.EqualsBuilder;  
import org.apache.commons.lang.builder.HashCodeBuilder;  
import org.apache.commons.lang.builder.ReflectionToStringBuilder;  
import org.apache.commons.lang.builder.ToStringBuilder;  
public class BaseEntity  implements Serializable {  
 /\**  
       
  public String toString() {  
   return ReflectionToStringBuilder.toString(this);  
       }

       public boolean equals(Object other) {  
           if ( (this == other ) ) return true;  
           return EqualsBuilder.reflectionEquals(this, other);  
       }

       public int hashCode() {  
        return HashCodeBuilder.reflectionHashCode(this);  
       }  
}

\----------------------------

定义bean

package askyaya.entity;

import java.util.Date;

public class VisitStatInfoBean extends BaseEntity {  
 /\**  
  *  
  \*/  
 private static final long serialVersionUID = 1L;  
 String visitor\_ip;  
 String server\_ip;  
 String page\_url;  
 String parameter;  
 String Referer\_page;  
 int Visitor\_type;  
 int server\_id;  
 int column\_id;  
 int page\_id;  
 int Visit\_count;  
 int User\_id;  
 int Product\_id;  
 int Seller\_id;  
 Date info\_date;  
 public Date getInfo\_date() {  
  return info\_date;  
 }  
 public void setInfo\_date(Date info\_date) {  
  this.info\_date = info\_date;  
 }  
 public int getColumn\_id() {  
  return column\_id;  
 }  
 public void setColumn\_id(int column\_id) {  
  this.column\_id = column\_id;  
 }  
 public int getPage\_id() {  
  return page\_id;  
 }  
 public void setPage\_id(int page\_id) {  
  this.page\_id = page\_id;  
 }  
 public String getPage\_url() {  
  return page\_url;  
 }  
 public void setParameter(String parameter) {  
  this.parameter = parameter;  
 }  
 public String getParameter() {  
  return parameter;  
 }  
 public void setPage\_url(String parameter) {  
  this.parameter = parameter;  
 }  
   
 public int getProduct\_id() {  
  return Product\_id;  
 }  
 public void setProduct\_id(int product\_id) {  
  Product\_id = product\_id;  
 }  
 public String getReferer\_page() {  
  return Referer\_page;  
 }  
 public void setReferer\_page(String referer\_page) {  
  Referer\_page = referer\_page;  
 }  
 public int getSeller\_id() {  
  return Seller\_id;  
 }  
 public void setSeller\_id(int seller\_id) {  
  Seller\_id = seller\_id;  
 }  
 public int getServer\_id() {  
  return server\_id;  
 }  
 public void setServer\_id(int server\_id) {  
  this.server\_id = server\_id;  
 }  
 public String getServer\_ip() {  
  return server\_ip;  
 }  
 public void setServer\_ip(String server\_ip) {  
  this.server\_ip = server\_ip;  
 }  
 public int getUser\_id() {  
  return User\_id;  
 }  
 public void setUser\_id(int user\_id) {  
  User\_id = user\_id;  
 }  
 public int getVisit\_count() {  
  return Visit\_count;  
 }  
 public void setVisit\_count(int visit\_count) {  
  Visit\_count = visit\_count;  
 }  
 public String getVisitor\_ip() {  
  return visitor\_ip;  
 }  
 public void setVisitor\_ip(String visitor\_ip) {  
  this.visitor\_ip = visitor\_ip;  
 }  
 public int getVisitor\_type() {  
  return Visitor\_type;  
 }  
 public void setVisitor\_type(int visitor\_type) {  
  Visitor\_type = visitor\_type;  
 }  
 @Override  
 public int hashCode() {  
  final int PRIME = 31;  
  int result = super.hashCode();  
  result = PRIME * result + Product\_id;  
  result = PRIME * result + ((Referer\_page == null) ? 0 : Referer\_page.hashCode());  
  result = PRIME * result + Seller\_id;  
  result = PRIME * result + User\_id;  
  result = PRIME * result + Visit\_count;  
  result = PRIME * result + Visitor\_type;  
  result = PRIME * result + column\_id;  
  result = PRIME * result + ((info\_date == null) ? 0 : info\_date.hashCode());  
  result = PRIME * result + page\_id;  
  result = PRIME * result + ((page\_url == null) ? 0 : page\_url.hashCode());  
  result = PRIME * result + server\_id;  
  result = PRIME * result + ((server\_ip == null) ? 0 : server\_ip.hashCode());  
  result = PRIME * result + ((visitor\_ip == null) ? 0 : visitor\_ip.hashCode());  
  return result;  
 }  
 @Override  
 public boolean equals(Object obj) {  
  if (this == obj)  
   return true;  
  if (!super.equals(obj))  
   return false;  
  if (getClass() != obj.getClass())  
   return false;  
  final VisitStatInfoBean other = (VisitStatInfoBean) obj;  
  if (Product\_id != other.Product\_id)  
   return false;  
  if (Referer\_page == null) {  
   if (other.Referer\_page != null)  
    return false;  
  } else if (!Referer\_page.equals(other.Referer\_page))  
   return false;  
  if (Seller\_id != other.Seller\_id)  
   return false;  
  if (User\_id != other.User\_id)  
   return false;  
  if (Visit\_count != other.Visit\_count)  
   return false;  
  if (Visitor\_type != other.Visitor\_type)  
   return false;  
  if (column\_id != other.column\_id)  
   return false;  
  if (info\_date == null) {  
   if (other.info\_date != null)  
    return false;  
  } else if (!info\_date.equals(other.info\_date))  
   return false;  
  if (page\_id != other.page\_id)  
   return false;  
  if (page\_url == null) {  
   if (other.page\_url != null)  
    return false;  
  } else if (!page\_url.equals(other.page\_url))  
   return false;  
  if (server\_id != other.server\_id)  
   return false;  
  if (server\_ip == null) {  
   if (other.server\_ip != null)  
    return false;  
  } else if (!server\_ip.equals(other.server\_ip))  
   return false;  
  if (visitor\_ip == null) {  
   if (other.visitor\_ip != null)  
    return false;  
  } else if (!visitor\_ip.equals(other.visitor\_ip))  
   return false;  
  return true;  
 }  
}  
\----------------------------

 

 public static void recordVisit\_JMS\_Info(String visitor\_ip,String server\_ip,String page\_url, String parameter, String Referer\_page,  
       int Visitor\_type,int server\_id,int column\_id,int page\_id,int Visit\_count,int User\_id,  
       int Product\_id,int Seller\_id)  
   {     
  //从页面上record\_visit.jsp 点击中,触发了这个方法      
   VisitStatInfoBean visitStatInfoBean=new VisitStatInfoBean();  
   visitStatInfoBean.setVisitor\_ip(visitor\_ip);  
   visitStatInfoBean.setServer\_ip(server\_ip);  
   visitStatInfoBean.setPage\_url(page\_url);  
   visitStatInfoBean.setReferer\_page(Referer\_page);  
   visitStatInfoBean.setVisitor\_type(Visitor\_type);  
   visitStatInfoBean.setServer\_id(server\_id);  
   visitStatInfoBean.setColumn\_id(column\_id);  
   visitStatInfoBean.setPage\_id(page\_id);  
   visitStatInfoBean.setVisit\_count(Visit\_count);  
   visitStatInfoBean.setUser\_id(User\_id);  
   visitStatInfoBean.setProduct\_id(Product\_id);  
   visitStatInfoBean.setSeller\_id(Seller\_id);  
   visitStatInfoBean.setInfo\_date(new Date());        
     //VisitLogSender producer=new VisitLogSender();  
     
   try {  
    VisitLogSender visitLogSender = VisitLogSender.getVisitLogSender();  
      
    visitLogSender.sendMessage(visitStatInfoBean);  
    //producer.close();  
   } catch (Exception e) {  
    e.printStackTrace();  
    // TODO: handle exception  
   }  
   
     
     }

 

..............

  关键点是下面的

  VisitLogSender visitLogSender = VisitLogSender.getVisitLogSender(); //单例  
      
    visitLogSender.sendMessage(visitStatInfoBean); //发送消息

..........

\------------------------

package askyaya.jms;

import javax.jms.Connection;  
import javax.jms.DeliveryMode;  
import javax.jms.Destination;  
import javax.jms.JMSException;  
import javax.jms.MessageProducer;  
import javax.jms.ObjectMessage;  
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

import askyaya.entity.VisitStatInfoBean;  
import askyaya.visitlog.GetJmsUrl;

/\**  
\* @author 作者 Administrator:金果  
\* @version 创建时间：  
\* 类说明:  
\*/  
public class VisitLogSender {

    private static  String subject = "askyaya.visit.log";   
     
    private static Destination destination = null;   //消息的目的地  
      
    private static  Connection connection = null;   //JMS 客户端到JMS Provider 的连接  
     
    private static Session session = null;    //一个发送或接收消息的线程  
     
    private static  MessageProducer producer = null;  //由Session 对象创建的用来发送消息的对象  
    
    private static VisitLogSender visitLogSender = null;  
     
    public static VisitLogSender getVisitLogSender(){  
      if(visitLogSender == null){  
       visitLogSender = new VisitLogSender();         
       try{  
        visitLogSender.initialize();  
       }catch(Exception e)  
       {e.printStackTrace();}  
      }        
      return visitLogSender;  
     }  
       
  public  void   initialize() throws JMSException, Exception {  
        
      System.out.println("url-----------&gt;"+GetJmsUrl.getUrl());  
         
        ActiveMQConnectionFactory connectionFactory=new ActiveMQConnectionFactory("","",GetJmsUrl.getUrl());  
          
        connection = connectionFactory.createConnection();  
         connection.start();  
         session = connection.createSession(false, Session.AUTO\_ACKNOWLEDGE); //不用事务了,session以自动方式接收,通用性好  
         destination = session.createQueue(subject);     
         producer = session.createProducer(destination);   

         producer.setDeliveryMode(DeliveryMode.PERSISTENT); //以持久的方式到队列oracle 数据库  
          
     }   
     
     
     
     //发送消息    
     public void sendMessage(VisitStatInfoBean message) throws JMSException,Exception {  
              
         ObjectMessage msg=session.createObjectMessage();          
         msg.setObject(message);                     
         System.out.println("Producer:-&gt;Sending askyaya\_2007 message:"+message);  
         System.out.println("Producer:-&gt;Sending askyaya\_2007 msg:"+msg);          
         producer.send(msg);  
         System.out.println("Producer:-&gt;askyaya\_2007 Message sent complete!");  
        
     }     
     
     // 关闭连接   
    public void close() throws JMSException {     
         System.out.println("Producer:-&gt;Closing connection");     
         if (producer != null)     
             producer.close();     
         if (session != null)     
             session.close();     
         if (connection != null)    
             connection.close();     
     }  

 }

\----------------------------------------------

 开始收了啊

我是另外建了工程

 

package askyaya.jms;

import javax.jms.Connection;  
import javax.jms.Destination;  
import javax.jms.JMSException;  
import javax.jms.Message;  
import javax.jms.MessageConsumer;  
import javax.jms.MessageListener;  
import javax.jms.ObjectMessage;  
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;  
import org.apache.activemq.ActiveMQConnectionFactory;

import askyaya.dao.VisitStatInfoDao;  
import askyaya.entity.VisitStatInfoBean;  
import askyaya.util.GetJmsUrl;

/\**  
\* @author 作者 Administrator:jinguo  
\* @version 创建时间：2008-8-30 上午10:55:47  
\* 类说明:  
\*/  
public class ReceiveVisitLog implements MessageListener{

  // private String user =ActiveMQConnection.DEFAULT\_USER;     
     
  // private String password=ActiveMQConnection.DEFAULT\_PASSWORD;     
    
   // private String url=ActiveMQConnection.DEFAULT\_BROKER\_URL;     
    
    private static String subject = "askyaya.visit.log";     
    
    private static Destination destination = null;     
    
    private static Connection connection = null;     
    
    private static  Session session = null;     
    
    private static MessageConsumer consumer = null;  
     
    
     
  //  private MessageProducer replyProducer; //反馈信息  
     
    //连接connection，get session  
     private static ReceiveVisitLog receivevisitlog=null;  
      
     public static ReceiveVisitLog getReceiveVisitLog(){  
      if(receivevisitlog==null){  
       receivevisitlog=new  ReceiveVisitLog();  
       try {  
       initialize();  
   } catch (Exception e) {  
    e.printStackTrace();  
    // TODO: handle exception  
   }        
      }  
      return receivevisitlog;       
     }  
     
     
    private static  void initialize() throws JMSException{  
       
      GetJmsUrl url=new GetJmsUrl();  
      //ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user,password,url);  
     System.out.println("url---M:"+url.getUrl());  
      
        
      ActiveMQConnectionFactory connectionFactory=new ActiveMQConnectionFactory("","",url.getUrl());  
         System.out.println("connect to  tcp:192.168.0.245 ");  
         Connection connection=connectionFactory.createConnection();  
         connection.start();  
          
         session = connection.createSession(false, Session.AUTO\_ACKNOWLEDGE);          
         destination=session.createQueue(subject);  
     //  Destination advisoryDestination = AdvisorySupport.getProducerAdvisoryTopic(destination)  
         consumer=session.createConsumer(destination);  
       
    }  
     
    // 消费消息     
    public void consumeMessage() throws JMSException,Exception {  
    
       //开始监听         
       // MessageListenerForOrgMsg msgListener=new MessageListenerForOrgMsg();    
       consumer.setMessageListener(this);  
        
       System.out.println("Consumer:-&gt;after listener message...");  
        
      //如果想要去想主动的去接受消息,起用下面的         
      //Message message=consumer.receiveNoWait();         
      //TextMessage message=(TextMessage)consumer.receive(1000);  
        
  }     
       
   
    // 消息处理函数(onMessage是个构造函数继承,consumerTool开始执行的时候,先要初始化它,然后才initialze())  
    //由 container 选择一个实例  
 public void onMessage(Message message){  
  try {     
    
     System.out.println("Consumer:-&gt;Begin receive message---&gt;:"+message);  
       if(message!=null){  
      if(message instanceof ObjectMessage){  
      ObjectMessage  objmsg=(ObjectMessage)message;     
      //从消息中提取对象,转化为bean对象  
       VisitStatInfoBean visitinfo=(VisitStatInfoBean)objmsg.getObject();  
       
      //隔离对数据库的直接访问,  
      VisitStatInfoDao vistdao=new VisitStatInfoDao();  
      //向数据库中插入消息  
      vistdao.insert(visitinfo);  
      System.out.println("Consumer:-&gt;after insert messagedb");  
     }  
       }  
      
        
   //消费消息中在onMessage()方法中接收producer发送过来的消息进行处理，并可以通过replyProducer反馈信息给producer  
   /\*if(message.getJMSReplyTo()!=null){      
     replyProducer.send(message.getJMSReplyTo(),session.createTextMessage("Reply: "+message.getJMSMessageID()));     
   }\*/  
     
  } catch (Exception e) {  
   e.printStackTrace();  
     
  }  
    
 }  
   
  // 关闭连接     
    public void close() throws JMSException {     
        System.out.println("Consumer:-&gt;Closing connection");     
        if (consumer != null)     
            consumer.close();     
        if (session != null)     
            session.close();     
        if (connection != null)    
            connection.close();     
    }     
     
     
    public static void main(String\[] args) {

  //取得接收信息  
  ReceiveVisitLog receiveVisitLog = ReceiveVisitLog.getReceiveVisitLog();  
   GetJmsUrl url=new GetJmsUrl();   
    
  //接收信息时间间隔    
  //int sleepTime = 30\*1000;  
  int sleepTime=Integer.parseInt(url.getSleepTime());  
    
  while (true){                         
         try {  
          System.out.println("睡眠" + sleepTime/1000 + "秒之后, 开始接收消息......");  
          receiveVisitLog.consumeMessage();

             Thread.sleep(sleepTime);  
             System.out.println("接收消息完毕.");  
              
   } catch (Exception e) {  
    e.printStackTrace();  
      
   }    
  }  
   
 }  
     
     
}

\--------------------------------

为了方便大家 我把dao 方法也贴了出来

package askyaya.dao;

import java.sql.Connection;  
import java.sql.PreparedStatement;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;

import askyaya.entity.VisitStatInfoBean;

public class VisitStatInfoDao {  
    
 public  void insert(VisitStatInfoBean vfBean)throws SQLException{  
    
   Connection ApplicationConn=askyaya.util.DBManager.getconn();  
    String strsql="insert into tbl\_visit\_stat\_info(visitor\_ip,server\_id,server\_ip,column\_id,"  
    + "page\_id,page\_url, parameter, Visit\_count,User\_id,Visitor\_type,Product\_id,Seller\_id,"  
    + "Info\_date,visit\_date,Referer\_page) values(?,?,?,?,?,?,?,?,?,?,?,?,Sysdate,to\_char(Sysdate,'yyyy-mm-dd'),?)";  
    
    PreparedStatement pstmt = null;  
    ApplicationConn.setAutoCommit(false);  
    try {     
     pstmt = ApplicationConn.prepareStatement(strsql);            
   //VisitStatInfoBean vfBean=new VisitStatInfoBean();  
    pstmt.setString(1,vfBean.getVisitor\_ip());  
        pstmt.setInt(2, vfBean.getServer\_id());  
        pstmt.setString(3,vfBean.getServer\_ip());  
        pstmt.setInt(4,vfBean.getColumn\_id());  
        pstmt.setInt(5,vfBean.getPage\_id());  
        pstmt.setString(6,vfBean.getPage\_url());  
        pstmt.setString(7,vfBean.getParameter());  
        pstmt.setInt(8, vfBean.getVisit\_count());  
        pstmt.setInt(9,vfBean.getUser\_id());  
        pstmt.setInt(10,vfBean.getVisitor\_type());  
        pstmt.setInt(11,vfBean.getProduct\_id());  
        pstmt.setInt(12,vfBean.getSeller\_id());  
        pstmt.setString(13,vfBean.getReferer\_page());          
        pstmt.executeUpdate();   
        ApplicationConn.commit();  
       // System.out.println("insert the tbl\_visit\_stat\_info is end ");  
         } catch (SQLException sqle) {  
         int errcode = sqle.getErrorCode();  
           // System.err.println("aq.executeQuery: " + sqle.getMessage()+":;");           
              //违反唯一约束  
              if(errcode == 1){}         
              else{  
                // System.err.println("aq.executeQuery: " + sqle.getMessage()+":;");          
              }                    
          ApplicationConn.rollback();  
         }finally {  
     try {        
      pstmt.close();  
      ApplicationConn.close();       
     } catch (Exception ex) {  
      ex.printStackTrace();  
     }  
    }  
   
 }  
}  
\---------------------------------

得到地址的通用方法

package askyaya.util;

import java.io.InputStream;  
import java.util.Properties;

 

public   class  GetJmsUrl {  
     
 private final static  String strUrl=null;  
 private final static String sleeptime=null;  
    
 private final    Properties props=new Properties();  
   
 private final String url="askyayaJmsConstant.properties";  
    
 public GetJmsUrl(){  
      
 }    
   
 public  String getUrl(){    
  InputStream is=getClass().getResourceAsStream(url);    
     try {  
      if(strUrl==null){  
      props.load(is);  
      }  
  } catch (Exception e) {  
   System.err.println("Can not read the Properties file " + url);     
  }         
    String strUrl= props.getProperty("hostname");          
  return strUrl;  
    
 }  
   
 public   String  getSleepTime(){    
  InputStream is=getClass().getResourceAsStream(url);  
  try {  
   if(sleeptime==null){  
        props.load(is);         
   }   
  } catch (Exception e) {  
   e.printStackTrace();  
   // TODO: handle exception  
  }  
  String   strSleepTime= props.getProperty("sleepTime");  
    
  return strSleepTime;   
   
 }  
   
   
}  
\------------------------

askyayaJmsConstant.properties

设置如下:

 

hostname=tcp://192.168.0.245:61616?wireFormat.maxInactivityDuration=0  
   
 sleepTime=30000

\---------------

我把服务器的activemq.xml 配置也贴出来把,很关键的哟,不懂可以问我.

 

&lt;!--  
    Licensed to the Apache Software Foundation (ASF) under one or more  
    contributor license agreements.  See the NOTICE file distributed with  
    this work for additional information regarding copyright ownership.  
    The ASF licenses this file to You under the Apache License, Version 2.0  
    (the "License"); you may not use this file except in compliance with  
    the License.  You may obtain a copy of the License at  
    
    [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)  
    
    Unless required by applicable law or agreed to in writing, software  
    distributed under the License is distributed on an "AS IS" BASIS,  
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
    See the License for the specific language governing permissions and  
    limitations under the License.  
\--&gt;  
&lt;!-- START SNIPPET: example --&gt;  
&lt;beans  
  xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"  
  xmlns:amq="[http://activemq.apache.org/schema/core](http://activemq.apache.org/schema/core)"  
  xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
  xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-2.0.xsd](http://www.springframework.org/schema/beans/spring-beans-2.0.xsd)  
  [http://activemq.apache.org/schema/core](http://activemq.apache.org/schema/core) [http://activemq.apache.org/schema/core/activemq-core.xsd](http://activemq.apache.org/schema/core/activemq-core.xsd)    
  [http://activemq.apache.org/camel/schema/spring](http://activemq.apache.org/camel/schema/spring) [http://activemq.apache.org/camel/schema/spring/camel-spring.xsd](http://activemq.apache.org/camel/schema/spring/camel-spring.xsd)"&gt;

    &lt;!-- Allows us to use system properties as variables in this configuration file --&gt;  
    &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/&gt;

    &lt;broker xmlns="[http://activemq.apache.org/schema/core](http://activemq.apache.org/schema/core)" brokerName="localhost" dataDirectory="${activemq.base}/data"&gt;

        &lt;!-- Destination specific policies using destination names or wildcards --&gt;  
        &lt;destinationPolicy&gt;  
            &lt;policyMap&gt;  
                &lt;policyEntries&gt;  
                    &lt;policyEntry queue="&gt;" memoryLimit="5mb"/&gt;  
                    &lt;policyEntry topic="&gt;" memoryLimit="5mb"&gt;  
                        &lt;dispatchPolicy&gt;  
                            &lt;strictOrderDispatchPolicy/&gt;  
                        &lt;/dispatchPolicy&gt;  
                        &lt;subscriptionRecoveryPolicy&gt;  
                            &lt;lastImageSubscriptionRecoveryPolicy/&gt;  
                        &lt;/subscriptionRecoveryPolicy&gt;  
                    &lt;/policyEntry&gt;  
                &lt;/policyEntries&gt;  
            &lt;/policyMap&gt;  
        &lt;/destinationPolicy&gt;

        &lt;!-- Use the following to configure how ActiveMQ is exposed in JMX --&gt;  
        &lt;managementContext&gt;  
            &lt;managementContext createConnector="false"/&gt;  
        &lt;/managementContext&gt;

        &lt;!-- The store and forward broker networks ActiveMQ will listen to --&gt;  
        &lt;networkConnectors&gt;  
            &lt;!-- by default just auto discover the other brokers --&gt;  
            &lt;networkConnector name="default-nc" uri="multicast://default"/&gt;  
            &lt;!-- Example of a static configuration:  
            &lt;networkConnector name="host1 and host2" uri="static://(tcp://host1:61616,tcp://host2:61616)"/&gt;  
            --&gt;  
        &lt;/networkConnectors&gt;  
        &lt;!--  
        &lt;persistenceAdapter&gt;  
            &lt;amqPersistenceAdapter syncOnWrite="false" directory="${activemq.base}/data" maxFileLength="20 mb"/&gt;  
        &lt;/persistenceAdapter&gt;  
         --&gt;  
        &lt;!-- Use the following if you wish to configure the journal with JDBC --&gt;  
        &lt;!--  
        &lt;persistenceAdapter&gt;  
            &lt;journaledJDBC dataDirectory="${activemq.base}/data" dataSource="#postgres-ds"/&gt;  
        &lt;/persistenceAdapter&gt;  
        --&gt;

        &lt;!-- Or if you want to use pure JDBC without a journal --&gt;  
        
        &lt;persistenceAdapter&gt;  
            &lt;jdbcPersistenceAdapter dataSource="#oracle-ds"/&gt;  
        &lt;/persistenceAdapter&gt;  
       

        &lt;!--  The maximum about of space the broker will use before slowing down producers --&gt;  
        &lt;systemUsage&gt;  
            &lt;systemUsage&gt;  
                &lt;memoryUsage&gt;  
                    &lt;memoryUsage limit="20 mb"/&gt;  
                &lt;/memoryUsage&gt;  
                &lt;storeUsage&gt;  
                    &lt;storeUsage limit="1 gb" name="foo"/&gt;  
                &lt;/storeUsage&gt;  
                &lt;tempUsage&gt;  
                    &lt;tempUsage limit="100 mb"/&gt;  
                &lt;/tempUsage&gt;  
            &lt;/systemUsage&gt;  
        &lt;/systemUsage&gt;

        &lt;!-- The transport connectors ActiveMQ will listen to --&gt;  
        &lt;transportConnectors&gt;  
            &lt;transportConnector name="openwire" uri="tcp://192.168.0.245:61616?wireFormat.maxInactivityDuration=0" discoveryUri="multicast://default"/&gt;  
            &lt;transportConnector name="ssl" uri="ssl://192.168.0.245:61617"/&gt;  
            &lt;transportConnector name="stomp" uri="stomp://192.168.0.245:61613"/&gt;  
            &lt;transportConnector name="xmpp" uri="xmpp://192.168.0.245:61222"/&gt;  
        &lt;/transportConnectors&gt;

    &lt;/broker&gt;

    &lt;!--  
    \** Lets deploy some Enterprise Integration Patterns inside the ActiveMQ Message Broker  
    \** For more details see  
    \**  
    \** [http://activemq.apache.org/enterprise-integration-patterns.html](http://activemq.apache.org/enterprise-integration-patterns.html)  
    --&gt;  
    &lt;camelContext id="camel" xmlns="[http://activemq.apache.org/camel/schema/spring](http://activemq.apache.org/camel/schema/spring)"&gt;

        &lt;!-- You can use a &lt;package&gt; element for each root package to search for Java routes --&gt;  
        &lt;package&gt;org.foo.bar&lt;/package&gt;

        &lt;!-- You can use Spring XML syntax to define the routes here using the &lt;route&gt; element --&gt;  
        &lt;route&gt;  
            &lt;from uri="activemq:example.A"/&gt;  
            &lt;to uri="activemq:example.B"/&gt;  
        &lt;/route&gt;  
    &lt;/camelContext&gt;

    &lt;!-- Uncomment to create a command agent to respond to message based admin commands on the ActiveMQ.Agent topic --&gt;  
    &lt;!--  
    &lt;commandAgent xmlns="[http://activemq.apache.org/schema/core](http://activemq.apache.org/schema/core)" brokerUrl="vm://localhost"/&gt;  
    --&gt;

    &lt;!-- An embedded servlet engine for serving up the Admin console --&gt;  
    &lt;jetty xmlns="[http://mortbay.com/schemas/jetty/1.0](http://mortbay.com/schemas/jetty/1.0)"&gt;  
        &lt;connectors&gt;  
            &lt;nioConnector port="8161"/&gt;  
        &lt;/connectors&gt;

        &lt;handlers&gt;  
            &lt;webAppContext contextPath="/admin" resourceBase="${activemq.base}/webapps/admin" logUrlOnStart="true"/&gt;  
            &lt;webAppContext contextPath="/demo" resourceBase="${activemq.base}/webapps/demo" logUrlOnStart="true"/&gt;  
            &lt;webAppContext contextPath="/fileserver" resourceBase="${activemq.base}/webapps/fileserver" logUrlOnStart="true"/&gt;  
        &lt;/handlers&gt;  
    &lt;/jetty&gt;

    &lt;!--  This xbean configuration file supports all the standard spring xml configuration options --&gt;

    &lt;!-- Postgres DataSource Sample Setup --&gt;  
    &lt;!--  
    &lt;bean id="postgres-ds" class="org.postgresql.ds.PGPoolingDataSource"&gt;  
      &lt;property name="serverName" value="localhost"/&gt;  
      &lt;property name="databaseName" value="activemq"/&gt;  
      &lt;property name="portNumber" value="0"/&gt;  
      &lt;property name="user" value="activemq"/&gt;  
      &lt;property name="password" value="activemq"/&gt;  
      &lt;property name="dataSourceName" value="postgres"/&gt;  
      &lt;property name="initialConnections" value="1"/&gt;  
      &lt;property name="maxConnections" value="10"/&gt;  
    &lt;/bean&gt;  
    --&gt;

    &lt;!-- MySql DataSource Sample Setup --&gt;  
    &lt;!--  
    &lt;bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;  
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"/&gt;  
      &lt;property name="url" value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"/&gt;  
      &lt;property name="username" value="activemq"/&gt;  
      &lt;property name="password" value="activemq"/&gt;  
      &lt;property name="maxActive" value="200"/&gt;  
      &lt;property name="poolPreparedStatements" value="true"/&gt;  
    &lt;/bean&gt;  
    --&gt;

    &lt;!-- Oracle DataSource Sample Setup --&gt;  
    
    &lt;bean id="oracle-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;  
      &lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/&gt;  
      &lt;property name="url" value="jdbc:oracle:thin:@192.168.0.245:1521:askyaya"/&gt;  
      &lt;property name="username" value="activemq"/&gt;  
      &lt;property name="password" value="activemq"/&gt;  
      &lt;property name="maxActive" value="200"/&gt;  
      &lt;property name="poolPreparedStatements" value="true"/&gt;  
    &lt;/bean&gt;  
   

    &lt;!-- Embedded Derby DataSource Sample Setup --&gt;  
    &lt;!--  
    &lt;bean id="derby-ds" class="org.apache.derby.jdbc.EmbeddedDataSource"&gt;  
      &lt;property name="databaseName" value="derbydb"/&gt;  
      &lt;property name="createDatabase" value="create"/&gt;  
    &lt;/bean&gt;  
    --&gt;

&lt;/beans&gt;  
&lt;!-- END SNIPPET: example --&gt;

 

\----------------------

以上我用jdbc 来写的,速度非常快,也可以hibernate实现如果有大虾知道的话贴出来 ,

最后吗,有些人问我,什么样的人适合做程序员,告诉大家

就是突破能力一定要强,想锻炼这能力,多看看书哟.以上实现我只用一个星期就基本就解决了jms 大部分难点.