网站域名注册免费,公积金网站建设方案,小型网站建设教程,好看的学校网站模板免费下载消息传递是用于构建不同级别的分布式软件系统的极其强大的工具。 通常#xff0c;至少在Java生态系统中#xff0c;客户端#xff08;前端#xff09;从不直接与消息代理#xff08;或交换#xff09;进行交互#xff0c;而是通过调用服务器端#xff08;后端#xff… 消息传递是用于构建不同级别的分布式软件系统的极其强大的工具。 通常至少在Java生态系统中客户端前端从不直接与消息代理或交换进行交互而是通过调用服务器端后端服务来进行交互。 否则客户甚至可能不知道已安装了消息传递解决方案。 随着Websockets越来越被采用对诸如STOMP 用于与消息代理或交换进行通信之类的面向文本的协议的广泛支持将产生影响。 今天的帖子将尝试解释公开两个非常流行的JMS实现Apache ActiveMQ和JBoss HornetQ是多么简单这两个Web前端JavaScript可以使用基于Websockets的 STOMP来用于Web前端JavaScript。 在深入研究代码之前可能有人认为这样做不是一个好主意。 那目的是什么 答案确实取决于 您正在开发原型/概念证明并且需要简单的方法来集成发布/订阅或对等消息传递 您不需要/不需要构建复杂的体系结构而最简单的解决方案就足够了 可扩展性故障转移和许多其他非常重要的决定在这里没有考虑但是如果您正在开发健壮和有弹性的体系结构则绝对应该考虑。 因此让我们开始吧。 与往常一样最好从我们要解决的问题开始我们想开发一个简单的发布/订阅解决方案使用JavaScript编写的Web客户端能够发送消息并侦听特定主题。 只要收到任何消息客户端就会显示简单的警报窗口。 请注意我们需要使用支持Websocket的现代浏览器例如Google Chrome或Mozilla Firefox 。 对于我们的两个示例客户端的代码均保持不变因此我们从此开始。 最佳起点是STOMP Over WebSocket文章其中介绍了stomp.js模块这是我们的index.html script srcstomp.js/scriptscript typetext/javascriptvar 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!);});/script 非常简单的代码但很少有细节值得解释。 首先我们正在ws// localhost61614 / stomp寻找Websockets端点。 这足以进行本地部署但最好用真实IP地址或主机名替换localhost 。 其次一旦连接客户端就订阅该主题仅对优先级为9的消息感兴趣并在此之后立即将消息发布到该主题。 从客户的角度来看我们已经完成了。 让我们继续进行消息代理列表中的第一个是Apache ActiveMQ 。 为了简化示例我们将不使用配置XML文件将Apache ActiveMQ代理嵌入到简单的Spring应用程序中。 由于源代码在GitHub上可用 因此我将跳过POM文件片段仅显示代码 package com.example.messaging;import java.util.Collections;import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.jmx.ManagementContext;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.hooks.SpringContextHook;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;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 );return broker;}
} 如我们所见 ActiveMQ代理配置有ws// localhost61614连接器该连接器假定使用STOMP协议。 另外我们正在创建名称为jms.topic.test的 JMS主题并启用JMX管理工具。 并运行它简单的Starter类 package com.example.messaging;import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Starter {public static void main( String[] args ) {ApplicationContext context new AnnotationConfigApplicationContext( AppConfig.class );}
} 现在启动并运行它让我们在浏览器中打开index.html文件我们应该看到类似以下内容 简单 对于好奇的读者 ActiveMQ使用Jetty 7.6.7.v20120910来支持Websockets并且不能与最新的Jetty发行版一起使用。 接下来就HornetQ而言 实现看起来有些不同尽管也不是很复杂。 由于Starter类保持不变因此唯一的变化是配置 package com.example.hornetq;import java.util.Collections;
import java.util.HashMap;
import java.util.Map;import org.hornetq.api.core.TransportConfiguration;
import org.hornetq.core.config.impl.ConfigurationImpl;
import org.hornetq.core.remoting.impl.netty.NettyAcceptorFactory;
import org.hornetq.core.remoting.impl.netty.TransportConstants;
import org.hornetq.core.server.JournalType;
import org.hornetq.jms.server.config.ConnectionFactoryConfiguration;
import org.hornetq.jms.server.config.JMSConfiguration;
import org.hornetq.jms.server.config.TopicConfiguration;
import org.hornetq.jms.server.config.impl.ConnectionFactoryConfigurationImpl;
import org.hornetq.jms.server.config.impl.JMSConfigurationImpl;
import org.hornetq.jms.server.config.impl.TopicConfigurationImpl;
import org.hornetq.jms.server.embedded.EmbeddedJMS;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;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 jmsServer;}
} 完整的源代码在GitHub上 。 在运行Starter类并在浏览器中打开index.html之后我们应该看到非常相似的结果 HornetQ配置看起来更加冗长但是除了出色的Netty框架之外没有涉及其他依赖项。 出于好奇我将ActiveMQ代理替换为Apollo实现。 尽管我成功实现了预期的功能但我发现该API非常麻烦至少在当前版本1.6中如此因此本文中没有涉及它。 参考 Andriy Redko {devmind}博客上的JCG合作伙伴 Andrey Redko 使用ActiveMQ和HornetQ通过WebSockets通过STOMP进行了简单消息传递 。 翻译自: https://www.javacodegeeks.com/2013/09/easy-messaging-with-stomp-over-websockets-using-activemq-and-hornetq.html