当前位置:  编程技术>移动开发
本页文章导读:
    ▪大型企业门户网站设计开发一般性准则和建议        大型企业门户网站设计开发一般性原则和建议 ......
    ▪ 基于Tomcat器皿的ActiveMQ的实例【转】        基于Tomcat容器的ActiveMQ的实例【转】 原文地址:http://topmanopensource.iteye.com/blog/1065633      在tomcat中配置ConnectionFactory和ActiveMQQueue,通过JNDI方式实现ActiveMQ。因为JNDI配置在        在tomca.........
    ▪ Spring调整JMS【转】       Spring整合JMS【转】 Spring整合JMS就像对orm, web的支持一样, spring同样支持jms, 为整合jms到已有的项目提供了很多便利的方法. 本篇主要讲实战, 是所以先从配置开始, spring配置jms基本上需要8个.........

[1]大型企业门户网站设计开发一般性准则和建议
    来源: 互联网  发布时间: 2014-02-18
大型企业门户网站设计开发一般性原则和建议

    
[2] 基于Tomcat器皿的ActiveMQ的实例【转】
    来源: 互联网  发布时间: 2014-02-18
基于Tomcat容器的ActiveMQ的实例【转】
原文地址:http://topmanopensource.iteye.com/blog/1065633
      在tomcat中配置ConnectionFactory和ActiveMQQueue,通过JNDI方式实现ActiveMQ。因为JNDI配置在 

      在tomcat中配置ConnectionFactory和ActiveMQQueue,通过JNDI方式实现ActiveMQ。因为JNDI配置在Java代码 
tomcat容器中所以必须通过jsp访问tomcat容器。 

tomcat容器中所以必须通过jsp访问tomcat容器。首先在在tomcat的lib目录添加如下jar文件。

Java代码 
<classpathentry kind="lib" path="src/activemq-all-5.5.0.jar"/>  
    <classpathentry kind="lib" path="src/slf4j-api-1.5.2-sources.jar"/>  
    <classpathentry kind="lib" path="src/slf4j-api-1.5.2.jar"/>  
    <classpathentry kind="lib" path="src/slf4j-simple-1.5.2.jar"/>  
    <classpathentry kind="lib" path="src/log4j-1.2.8.jar"/>  
    <classpathentry kind="lib" path="src/commons-dbcp-1.4.jar"/>  
    <classpathentry kind="lib" path="src/commons-pool-1.5.4.jar"/>  
    <classpathentry kind="lib" path="src/commons-collections-3.2.1.jar"/>  
    <classpathentry kind="lib" path="src/xbean-server-3.3.jar"/> 

<classpathentry kind="lib" path="src/activemq-all-5.5.0.jar"/>
<classpathentry kind="lib" path="src/slf4j-api-1.5.2-sources.jar"/>
<classpathentry kind="lib" path="src/slf4j-api-1.5.2.jar"/>
<classpathentry kind="lib" path="src/slf4j-simple-1.5.2.jar"/>
<classpathentry kind="lib" path="src/log4j-1.2.8.jar"/>
<classpathentry kind="lib" path="src/commons-dbcp-1.4.jar"/>
<classpathentry kind="lib" path="src/commons-pool-1.5.4.jar"/>
<classpathentry kind="lib" path="src/commons-collections-3.2.1.jar"/>
<classpathentry kind="lib" path="src/xbean-server-3.3.jar"/>

在Tomcat的conf目录下的context.xml配置中添加如下:

Java代码 
<Resource name="jms/ConnectionFactory"    
                auth="Container"    
                type="org.apache.activemq.ActiveMQConnectionFactory" 
                description="JMS Connection Factory" 
                factory="org.apache.activemq.jndi.JNDIReferenceFactory" 
                brokerURL="tcp://localhost:61616" 
                brokerName="LocalActiveMQBroker" />  
                  
    <Resource name="jms/Queue"    
                auth="Container"    
                type="org.apache.activemq.command.ActiveMQQueue" 
                description="My Queue" 
                factory="org.apache.activemq.jndi.JNDIReferenceFactory" 
                physicalName="TomcatQueue" />     

<Resource name="jms/ConnectionFactory" 
auth="Container" 
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="tcp://localhost:61616"
brokerName="LocalActiveMQBroker" />

<Resource name="jms/Queue" 
auth="Container" 
type="org.apache.activemq.command.ActiveMQQueue"
description="My Queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="TomcatQueue" /> 配置如下:

Java代码 
<?xml version="1.0" encoding="UTF-8"?>  
<!--  
    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  
     
    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.  
-->  
<!-- START SNIPPET: xbean -->  
<beans   
  xmlns="http://www.springframework.org/schema/beans"   
  xmlns:amq="http://activemq.apache.org/schema/core" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">  
  <!-- 连接连接工厂 -->  
<bean  id="jmsConnectionFactory" >  
     <property name="jndiName" value="java:comp/env/jms/ConnectionFactory"></property>  
</bean>  
   
   
<bean  id="tomcatQueue" >  
     <property name="jndiName" value="java:comp/env/jms/Queue"></property>  
</bean>  
 
<!-- 配置JMS的模板 -->  
<bean id="jmsTemplate"  >  
   <property name="connectionFactory" >  
     <ref  bean="jmsConnectionFactory"/>  
   </property>  
   <property name="defaultDestination">  
      <ref bean="tomcatQueue"/>  
   </property>  
</bean>  
 
 
<!-- 发送消息队列到目的地 -->  
<bean id="sender"  >  
    <property name="jmsTemplate">  
       <ref bean="jmsTemplate"/>  
    </property>  
</bean>  
<!-- 接收消息 -->  
<bean id="receiver" >  
    <property name="jmsTemplate">  
       <ref bean="jmsTemplate"/>  
    </property>  
</bean>  
 
 
<bean id="listenerContainer" >  
   <property name="connectionFactory">  
      <ref bean="jmsConnectionFactory"/>  
   </property>  
   <property name="destination">  
      <ref bean="tomcatQueue"/>  
   </property>  
   <property name="messageListener">  
      <ref bean="receiver"/>  
    </property>  
</bean>  
 
</beans> 

<?xml version="1.0" encoding="UTF-8"?>
<!--
    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
  
    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.
-->
<!-- START SNIPPET: xbean -->
<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:amq="http://activemq.apache.org/schema/core"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
  <!-- 连接连接工厂 -->
<bean  id="jmsConnectionFactory" >
     <property name="jndiName" value="java:comp/env/jms/ConnectionFactory"></property>
</bean>


<bean  id="tomcatQueue" >
     <property name="jndiName" value="java:comp/env/jms/Queue"></property>
</bean>

<!-- 配置JMS的模板 -->
<bean id="jmsTemplate"  >
   <property name="connectionFactory" >
     <ref  bean="jmsConnectionFactory"/>
   </property>
   <property name="defaultDestination">
      <ref bean="tomcatQueue"/>
   </property>
</bean>


<!-- 发送消息队列到目的地 -->
<bean id="sender"  >
    <property name="jmsTemplate">
       <ref bean="jmsTemplate"/>
    </property>
</bean>
<!-- 接收消息 -->
<bean id="receiver" >
    <property name="jmsTemplate">
       <ref bean="jmsTemplate"/>
    </property>
</bean>


<bean id="listenerContainer" >
   <property name="connectionFactory">
      <ref bean="jmsConnectionFactory"/>
   </property>
   <property name="destination">
      <ref bean="tomcatQueue"/>
   </property>
   <property name="messageListener">
      <ref bean="receiver"/>
    </property>
</bean>

</beans>



代码如下:

Java代码 
package easyway.activemq.app.demo2;  
 
import javax.jms.JMSException;  
import javax.jms.Message;  
import javax.jms.Session;  
 
import org.springframework.jms.core.JmsTemplate;  
import org.springframework.jms.core.MessageCreator;  
 
/** 
* tomcat和activemq整合 
* 消息发送者 
* @author longgangbai 

*/ 
public class MessageSender {  
  private JmsTemplate jmsTemplate;  
    
    
  public void send(final String text){  
      jmsTemplate.send(new MessageCreator(){  
 
        public Message createMessage(Session session) throws JMSException {  
            // TODO Auto-generated method stub  
            return session.createTextMessage(text);  
        }  
      });  
  }  
 
 
    public JmsTemplate getJmsTemplate() {  
        return jmsTemplate;  
    }  
      
      
    public void setJmsTemplate(JmsTemplate jmsTemplate) {  
        this.jmsTemplate = jmsTemplate;  
    }  


package easyway.activemq.app.demo2;

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

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

/**
* tomcat和activemq整合
* 消息发送者
* @author longgangbai
*
*/
public class MessageSender {
  private JmsTemplate jmsTemplate;
 
 
  public void send(final String text){
  jmsTemplate.send(new MessageCreator(){

public Message createMessage(Session session) throws JMSException {
// TODO Auto-generated method stub
return session.createTextMessage(text);
}
  });
  }


public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}


public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
}


Java代码 
package easyway.activemq.app.demo2;  
 
import javax.jms.JMSException;  
import javax.jms.Message;  
import javax.jms.MessageListener;  
import javax.jms.TextMessage;  
 
import org.springframework.jms.core.JmsTemplate;  
/** 
* 消息接收者 
* @author longgangbai 

*/ 
public class MessageReceiver implements  MessageListener {  
    private JmsTemplate jmsTemplate;  
        
    public JmsTemplate getJmsTemplate() {  
        return jmsTemplate;  
    }  
 
    public void setJmsTemplate(JmsTemplate jmsTemplate) {  
        this.jmsTemplate = jmsTemplate;  
    }  
    public void receive() throws JMSException{  
         TextMessage text=(TextMessage)this.jmsTemplate.receive();  
        System.out.println("receive="+text.getText());  
    }  
    public void onMessage(Message message) {  
         if(message instanceof TextMessage){  
             TextMessage text=(TextMessage)message;  
             try {  
                System.out.println(text.getText());  
            } catch (Exception e) {  
            }  
         }  
    }  
      


package easyway.activemq.app.demo2;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.springframework.jms.core.JmsTemplate;
/**
* 消息接收者
* @author longgangbai
*
*/
public class MessageReceiver implements  MessageListener {
private JmsTemplate jmsTemplate;
 
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}

public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
    public void receive() throws JMSException{
    TextMessage text=(TextMessage)this.jmsTemplate.receive();
System.out.println("receive="+text.getText());
    }
public void onMessage(Message message) {
if(message instanceof TextMessage){
TextMessage text=(TextMessage)message;
try {
System.out.println(text.getText());
} catch (Exception e) {
}
}
}
   
}


Java代码 
package easyway.activemq.app.demo2;  
 
import javax.jms.JMSException;  
 
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;  
/** 
* 测试类 
* @author longgangbai 

*/ 
public class MessageTest {  
    public void test() throws JMSException {  
        ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("app-activemq-tomcat.xml");  
        MessageSender sender=(MessageSender)ctx.getBean("sender");  
        MessageReceiver receive=(MessageReceiver)ctx.getBean("receiver");  
        sender.send("helloworld");  
        receive.receive();  
    }  
 


package easyway.activemq.app.demo2;

import javax.jms.JMSException;

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
/**
* 测试类
* @author longgangbai
*
*/
public class MessageTest {
public void test() throws JMSException {
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("app-activemq-tomcat.xml");
MessageSender sender=(MessageSender)ctx.getBean("sender");
MessageReceiver receive=(MessageReceiver)ctx.getBean("receiver");
sender.send("helloworld");
receive.receive();
}

}
index.jsp内容:

Java代码 
<%@ page language="java" import="easyway.activemq.app.demo2.*" pageEncoding="UTF-8"%>  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
    <base href="/blog_article/<%=basePath%>/index.html">  
      
    <title>My JSP 'index.jsp' starting page</title>  
    <meta http-equiv="pragma" content="no-cache">  
    <meta http-equiv="cache-control" content="no-cache">  
    <meta http-equiv="expires" content="0">      
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
    <meta http-equiv="description" content="This is my page">  
    <!--  
    <link rel="stylesheet" type="text/css" href="/blog_article/styles.css">  
    -->  
  </head>  
    
  <body>  
    
    <%  
        MessageTest message=new MessageTest();  
        message.test();  
     %>  
  <br>  
  </body>  
</html> 

    
[3] Spring调整JMS【转】
    来源: 互联网  发布时间: 2014-02-18
Spring整合JMS【转】
Spring整合JMS
就像对orm, web的支持一样, spring同样支持jms, 为整合jms到已有的项目提供了很多便利的方法. 本篇主要讲实战, 是所以先从配置开始, spring配置jms基本上需要8个部分.

ConnectionFactory. 和jms服务器的连接, 可以是外部的jms server, 也可以使用embedded ActiveMQ Broker.
Destination. 有topic和queue两种方式.
JmsTemplate. spring提供的jms模板.
MessageConverter. 消息转换器.
MessageProducer. 消息生产者.
MessageConsumer. 消息消费者.
MessageListener. 消息监听器
MessageListenerContainer. 消息监听容器
下面以实例的方式介绍上面8个部分.

1. ConnectionFactory
Xml代码
<amq:connectionFactory id="jmsConnectionFactory" brokerURL="vm://localhost" />  

<amq:connectionFactory id="jmsConnectionFactory" brokerURL="vm://localhost" />
brokerURL是指要连接的activeMQ server的地址, activeMQ提供了多种brokerURL, 集体可参见文档.一般我们使用嵌套的ActiveMQ server. 配置如下, 这个配置使用消息的存储机制, 服务器重启也不会丢失消息.

Xml代码
<!--   embedded ActiveMQ Broker -->  
    <amq:broker useJmx="false" persistent="true">  
        <amq:persistenceAdapter>  
            <amq:amqPersistenceAdapter directory="d:/amq"/>  
        </amq:persistenceAdapter>  
        <amq:transportConnectors>  
            <amq:transportConnector uri="tcp://localhost:61616" />  
                       <amq:transportConnector uri="vm://localhost:0" />  
        </amq:transportConnectors>  
    </amq:broker>  

<!--  embedded ActiveMQ Broker -->
<amq:broker useJmx="false" persistent="true">
  <amq:persistenceAdapter>
   <amq:amqPersistenceAdapter directory="d:/amq"/>
  </amq:persistenceAdapter>
  <amq:transportConnectors>
   <amq:transportConnector uri="tcp://localhost:61616" />
                       <amq:transportConnector uri="vm://localhost:0" />
  </amq:transportConnectors>
</amq:broker>
2. Destination
在实例中我们使用了两种destination

Xml代码
<!--   ActiveMQ destinations   -->  
<!--   使用topic方式-->  
<amq:topic name="TOPIC" physicalName="JMS-TEST-TOPIC" />  
<!--   使用Queue方式-->  
<amq:queue name="QUEUE" physicalName="JMS-TEST-QUEUE" />  

<!--  ActiveMQ destinations  -->
<!--  使用topic方式-->
<amq:topic name="TOPIC" physicalName="JMS-TEST-TOPIC" />
<!--  使用Queue方式-->
<amq:queue name="QUEUE" physicalName="JMS-TEST-QUEUE" />
3. JmsTemplate
Xml代码
<!--   Spring JmsTemplate config -->  
    <bean id="jmsTemplate" >  
        <property name="connectionFactory">  
            <!--   lets wrap in a pool to avoid creating a connection per send -->  
            <bean >  
                <property name="targetConnectionFactory" ref="jmsConnectionFactory" />  
            </bean>  
        </property>  
        <!-- custom MessageConverter -->  
        <property name="messageConverter" ref="defaultMessageConverter" />  
    </bean>  

<!--  Spring JmsTemplate config -->
<bean id="jmsTemplate" >
  <property name="connectionFactory">
   <!--  lets wrap in a pool to avoid creating a connection per send -->
   <bean >
    <property name="targetConnectionFactory" ref="jmsConnectionFactory" />
   </bean>
  </property>
  <!-- custom MessageConverter -->
  <property name="messageConverter" ref="defaultMessageConverter" />
</bean>
4. MessageConverter
   MessageConverter实现的是org.springframework.jms.support.converter.MessageConverter接口, 提供消息的转换功能. DefaultMessageConverter的实现见附件.

Xml代码
<bean id="defaultMessageConverter" />  


<amq:broker useJmx="false" persistent="true">
   <amq:persistenceAdapter>
         <amq:jdbcPersistenceAdapter id="jdbcAdapter" dataSource="#dataSource" createTablesOnStartup="true" useDatabaseLock="false"/>
      </amq:persistenceAdapter>
   <amq:transportConnectors>
    <amq:transportConnector uri="tcp://localhost:8888" />
   </amq:transportConnectors>
</amq:broker>


<amq:connectionFactory id="connectionFactoryConsumer"
   brokerURL="tcp://localhost:8888" />
 
<amq:connectionFactory id="connectionFactoryProducer"
   brokerURL="tcp://localhost:8888" />
 
<amq:queue name="destination" physicalName="JmsQuartzTest" />

<bean id="messageConverter" />

<bean id="jmsTemplate"
   >
   <property name="connectionFactory">
    <bean >
     <property name="targetConnectionFactory" ref="connectionFactoryProducer" />
    </bean>
   </property>
   <property name="messageConverter" ref="messageConverter"/>
</bean>



    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3