activemq使用笔记 和ibmmq 用哪个

是不是activeMQ可以主要传文件而webservice主要做消息通信?
大部分接口都用websense,但是当webservice调用需要很久才能完成时,这时候可以用mq,比如任务执行,不知道任务需要执行多久,启动任务接口可以用webservice,执行完可以通过mq返回结果
这个不是绝对的。
webservice主要还是做一个分布式,给不同的应用之间提供接口调用,
activeMQ:以前用过它做消息队列,就是服务器-&服务器之间的数据交互。
已解决问题
未解决问题使用Spring和ActiveMQ实现高效的轻量级的JMS
转载自:http://9834./blog/static//这是官方推荐要看的原文的翻译://efficient-lightweight-jms-with-spring-and-activemq/ 异步性(asynchronicity),是高伸缩性系统的首要的 设计 原则,对来说,这意味着JMS,然后又意味着ActiveMQ。但是我如何高效的使用JMS呢?一个人很有可能会不知所措,在谈论容器、框架、和一堆选项时。那么让我们分开来探讨下。框架ActiveMQ文档中提及两个框架:Camel和Spring。那么要做决定就取决于简单性和功能性的对比。Camel支持大量的企业集成模式(Enterprise Integration Patterns),它可以大大简化集成组件间的大量服务和复杂的消息流。如果你的系统需要这样的功能,它当然是非常好的选择。然而,如果你寻找简单性,仅仅支持基本的最佳实践,那么Spring是好的选择。JCA (使用它或放弃它) 阅读ActiveMQ的Spring支持时,者立刻被介绍了JCA容器和ActiveMQ很多的代理和适配器选项的概念。然而这些都不适应。JCA是EJB规范的一部分,对于大部分EJB规范,Spring都没有提供很好的支持。这时,就提到了Jencks,”为Spring提供的轻量级JCA容器”,它可以作为ActiveMQ的JCA容器。 乍看上去,这是个非常好的,但是我在这里告诉你不要这样做。Jencks最后更新时间是2007年1月。那时ActiveMQ的版本是 4.1x,Spring的版本是2.0.x,但是时间过了很多,事情也变化了很大。甚至是试图从Maven库中获取ActiveMQ4.1的依赖包 Jencks都会失败,因为它已经不存在了。简单的事实是有更好和更简单的方式来缓存资源。发送消息Spring消息发送的核心架构是JmsTemplate。在传统的Spring模板方式中,JmsTemplate隔离了像打开、关闭Session和Producer的繁琐操作,因此应用开发人员仅仅需要关注实际的业务逻辑。然而ActiveMQ快速的指出了JmsTemplate gotchas,大多是因为JmsTemplate被设计在每次调用时都打开和关闭session及producer。JmsTemplate损害了ActiveMQ的PooledConnectionFactory对session和消息producer的缓存机制而带来的性能提升。然而,这个太过时了。从Spring2.5.3开始,它开始提供自己的CachingConnectionFactory,我相信该类在缓存方面更加强大。然而,这里有一个点需要注意。默认情况下,CachingConnectionFactory只缓存一个session,在它的JavaDoc中,它声明对于低并发情况下这是足够的。与之相反,PooledConnectionFactory的默认值是500。这些设置,在很多情况下,需要亲自去测试并验证。我将其设置为100,对我来说还是很不错。接收消息可能你已经注意了,JmsTemplate gotchas强 烈的不建议你使用JmsTemplate的receive()调用,同样也是因为没有session和consumer的池机制。更多的原因是在 JmsTemplate上的所有调用都是同步的,这意味着调用线程需要被阻塞,直到方法返回。这在使用JmsTemplate发送消息时,没有任何问题, 因为该方法几乎是立即返回。然而,当调用receive方法时,线程将阻塞,直到接收到一个消息为止,这对性能影响很大。不幸的是,JmsTemplate gotchas和Spring支持文档中都没有对该问题提供简单的解决方案。实际上,它们均提倡使用Jencks,我们之前已经说过。实际的解决方案,使用DefaultListenerContainer,已经写在了how do I use JMS efficiently文 档中。DefaultMessageListenerContainer允许异步接收消息并缓存session和消息consumer。更有趣的 是,DefaultMessageListenerContainer可以根据消息数量的增加或缩减监听器的数量。简而言之,我们可以完全忽视 JCA。合并起来Spring Context文件&? xml
="1.0" encoding ="UTF-8"?&&beansxmlns="http://www.springframework.org/schema/beans"xmlns:amq="http://activemq..org/schema/core"xmlns:jms="http://www.springframework.org/schema/jms"xmlns:context="http://www.springframework.org/schema/context"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.5.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"&&!-- enables annotation based configuration --&&context:annotation-config/&&!-- scans
annotated classes in pany
--&&context:component-scanbase-package="net.sight"/&&!--
allows for ${} replacement in the spring xml configuration from the
system.properties file class="com">
--&&context:property-placeholderlocation="classpath:.properties"/&&!-- creates an activemq connection factory using the amq namespace --&&amq:connectionFactoryid="amqConnectionFactory"brokerURL="${jms.url}"userName="${jms.username}"password="${jms.password}"/&&!--
CachingConnectionFactory Definition, sessionCacheSize property is the
number of sessions to cache
--&&beanid="connectionFactory"class="org.springframework.jms.connection.CachingConnectionFactory"&&constructor-argref="amqConnectionFactory"/&&propertyname="exceptionListener"ref="jmsExceptionListener"/&&propertyname="sessionCacheSize"value="100"/&&/bean&&!-- JmsTemplate Definition --&&beanid="jmsTemplate"class="org.springframework.jms.core.JmsTemplate"&&constructor-argref="connectionFactory"/&&/bean&&!--
listener container definition using the jms namespace, concurrency is
the max number of concurrent listeners that can be started
--&&jms:listener-containerconcurrency="10"&&jms:listenerid="QueueListener"destination="Queue.Name"ref="queueListener"/&&/jms:listener-container&&/beans&这里有两点需要注意。首先,我添加了amq和jms的命名。第二,我使用了Spring2.5的基于注解的配置。通过使用基于注解的配置,我可以简单的添加@Component注解到我的Java类上,而不是使用XML文件显式的在spring中定义它们。另外,我可以在我的构造方法中使用@Autowired来将像JmsTemplate这样的对象注入到我的对象中。QueueSenderpackage net.javasight;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsTemplate;import org.springframework.stereotype.Component;@ComponentpublicclassQueueSender{privatefinalJmsTemplate jmsTemplate;@AutowiredpublicQueueSender(finalJmsTemplate jmsTemplate){this.jmsTemplate = jmsTemplate;}publicvoid send(finalString message){
jmsTemplate.convertAndSend("Queue.Name", message);}} Queue Listenerpackage net.javasight;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;import org.springframework.stereotype.Component;@ComponentpublicclassQueueListenerimplementsMessageListener{publicvoid class="pun">(finalMessage message){if(message instanceofTextMessage){finalTextMessage textMessage =(TextMessage) message;try{System.out.println(textMessage.getText());}catch(finalJMSException e){
e.printStackTrace();}}}} JmsExceptionListenerpackage net.javasight;import javax.jms.ExceptionListener;import javax.jms.JMSException;import org.springframework.stereotype.Component;@ComponentpublicclassJmsExceptionListenerimplementsExceptionListener{publicvoid class="pun">(finalJMSException e){
e.printStackTrace();}}当前访客身份:游客 [
已有文章 2182 篇
当前位置:
使用 ActiveMQ 和 HornetQ 在 WebSockets 上轻松实现 STOMP 消息传输
英文原文:
0人收藏此文章,
推荐于 2年前 (共 5 段, 翻译完成于 08-06) ()
参与翻译(1人):
对于在不同层级上构建分布式软件系统来说,消息机制是一个非常强大的工具。通常来说,至少在&Java 生态圈内,客户端 (前端) 从来不会直接和消息中间件(或交换器)&进行交互,而是去调用服务器端 (后端)的服务。或者说,客户端甚至都没有意识到还存在着消息解决方案。
随着&&得到了越来越多的使用,以及面向文本的协议的广泛支持,比如 (用来和消息中间件或交换器进行通讯) ,情况正在有所改变。&今天的这篇博文将试图解释下面的事情是多么容易,即通过使用基于的&协议,把两个非常流行的&&实现 Apache &和 JBoss &提供给&web 前端 (JavaScript) 来使用。
&翻译的不错哦!
在深入代码之前,也许有人会说这样做不是一个好主意。那么我们的目的是什么呢? 答案真的取决于:
你在开发的是概念的原型/证明,你需要一个简易的方式来整合发布/订阅或者点到点的消息发送
你不想要/需要 构建复杂精致的架构,最简单的能运行的解决方案就足够了
这里可扩展性,容错性和其它一些重要的决定因素都没有考虑在内,但是如果你想要开发具有鲁棒性和弹性的架构,这些绝对是应当考虑的。
好,让我们开始吧。从我们正在尝试解决的问题来入手总是更好一些: 我们想要开发一个简单的发布/订阅解决方案,来使那些用JavaScript 编写的&web 客户端可以发送消息并监听特定的主题。任何时候接收到任何消息时,客户端就会展示一个弹出窗口。请注意,我们需要使用现代浏览器,它要能够支持&,比如&&或者&.
&翻译的不错哦!
我们的两个例子中客户端代码都是一样的,所以我们就从那儿开始。一个很棒的学习起点是一篇叫做&&的文章,它介绍了&&组件,下面是我们的&index.html:
&script src="stomp.js"&&/script&
&script type="text/javascript"&
var client = Stomp.client( "ws://localhost:61614/stomp", "v11.stomp" );
client.connect( "", "",
function() {
client.subscribe("jms.topic.test",
function( message ) {
alert( message );
{ priority: 9 }
client.send("jms.topic.test", { priority: 9 }, "Pub/Sub over STOMP!");
很简单的代码,不过仍有几个细节值得解释一下。首先,我们在&ws://localhost:61614/stomp 寻找&&终端。在本机部署时这是没问题的,但是最好用真实的 IP 地址或者主机名来替代&localhost&。其次,一旦连接成功, 客户端会去订阅主题(只对优先级为 9的消息感兴趣) ,随后立即给该主题发送消息。从客户端的角度来看,我们已经完成了所有工作。
&翻译的不错哦!
让我们继续来看看消息中间件,首先是 Apache 。为了简化例子,我们会把&Apache &中间件嵌入到一个简单的不需要XML配置文件的&&应用中。因为源代码已经在&&上提供了,我将跳过 POM 文件片段,只展示代码部分:
package com.example.
import java.util.C
import org.apache.activemq.broker.BrokerS
import org.apache.activemq.broker.jmx.ManagementC
import org.mand.ActiveMQD
import org.mand.ActiveMQT
import org.apache.activemq.hooks.SpringContextH
import org.springframework.context.annotation.B
import org.springframework.context.annotation.C
@Configuration
public class AppConfig {
@Bean( initMethod = "start", destroyMethod = "stop" )
public BrokerService broker() throws Exception {
final BrokerService broker = new BrokerService();
broker.addConnector( "ws://localhost:61614" );
broker.setPersistent( false );
broker.setShutdownHooks( Collections.& Runnable &singletonList( new SpringContextHook() ) );
final ActiveMQTopic topic = new ActiveMQTopic( "jms.topic.test" );
broker.setDestinations( new ActiveMQDestination[] { topic }
final ManagementContext managementContext = new ManagementContext();
managementContext.setCreateConnector( true );
broker.setManagementContext( managementContext );
正如我们可以看到的,&中间件被配置为使用&ws://localhost:61614 链接,并假定它使用&&协议。并且,我们创建了名为&jms.topic.test&的 JMS 主题,启用了&JMX 管理。要运行它,使用&Starter&类:
package com.example.
import org.springframework.context.ApplicationC
import org.springframework.context.annotation.AnnotationConfigApplicationC
public class Starter
public static void main( String[] args ) {
ApplicationContext context = new AnnotationConfigApplicationContext( AppConfig.class );
现在,走起,开始运行,让我们在浏览器中打开&index.html&文件,我们应当可以看见类似下面的内容:
简单吧! 有求知欲的读者们请注意,为了支持&,&需要使用& 7.6.7.v&,最新的&&发布版不管用。
&翻译的不错哦!
继续,考虑到&&的特点,下面的实现看起来有些不同,不过仍然不太复杂。由于&Starter&类跟原来一样,所以唯一改变的是配置:
package com.example.
import java.util.C
import java.util.HashM
import java.util.M
import org.hornetq.api.core.TransportC
import org.hornetq.core.config.impl.ConfigurationI
import org.hornetq.core.remoting.impl.netty.NettyAcceptorF
import org.hornetq.core.remoting.impl.netty.TransportC
import org.hornetq.core.server.JournalT
import org.hornetq.jms.server.config.ConnectionFactoryC
import org.hornetq.jms.server.config.JMSC
import org.hornetq.jms.server.config.TopicC
import org.hornetq.jms.server.config.impl.ConnectionFactoryConfigurationI
import org.hornetq.jms.server.config.impl.JMSConfigurationI
import org.hornetq.jms.server.config.impl.TopicConfigurationI
import org.hornetq.jms.server.embedded.EmbeddedJMS;
import org.springframework.context.annotation.B
import org.springframework.context.annotation.C
@Configuration
public class AppConfig {
@Bean( initMethod = "start", destroyMethod = "stop" )
public EmbeddedJMS broker() throws Exception {
final ConfigurationImpl configuration = new ConfigurationImpl();
configuration.setPersistenceEnabled( false );
configuration.setJournalType( JournalType.NIO );
configuration.setJMXManagementEnabled( true );
configuration.setSecurityEnabled( false );
final Map& String, Object & params = new HashMap&&();
params.put( TransportConstants.HOST_PROP_NAME, "localhost" );
params.put( TransportConstants.PROTOCOL_PROP_NAME, "stomp_ws" );
params.put( TransportConstants.PORT_PROP_NAME, "61614" );
final TransportConfiguration stomp = new TransportConfiguration( NettyAcceptorFactory.class.getName(), params );
configuration.getAcceptorConfigurations().add( stomp );
configuration.getConnectorConfigurations().put( "stomp_ws", stomp );
final ConnectionFactoryConfiguration cfConfig = new ConnectionFactoryConfigurationImpl( "cf", true, "/cf" );
cfConfig.setConnectorNames( Collections.singletonList( "stomp_ws" ) );
final JMSConfiguration jmsConfig = new JMSConfigurationImpl();
jmsConfig.getConnectionFactoryConfigurations().add( cfConfig );
final TopicConfiguration topicConfig = new TopicConfigurationImpl( "test", "/topic/test" );
jmsConfig.getTopicConfigurations().add( topicConfig );
final EmbeddedJMS jmsServer = new EmbeddedJMS();
jmsServer.setConfiguration( configuration );
jmsServer.setJmsConfiguration( jmsConfig );
return jmsS
完整的源代码在&&上。在启动&Starter&类之后,在浏览器中打开&index.html,你将看到的结果应该类似于:
&的配置看起来似乎更啰嗦一些,不过除了优秀的&&框架之外,没有牵涉到其他的依赖。
出于我自己的好奇,我用&&实现替换了&&中间件。虽然我如期的使它正常运行,但是我发现它的&API 非常笨重,至少在当前的&1.6 版本中是这样,所以在本文中我没有叙及这部分内容。
所有源代码可以从GitHub上获取: &和&
&翻译的不错哦!

我要回帖

更多关于 activemq使用场景 的文章

 

随机推荐