apollo 参数传递_使用Apollo通过WebSocket通过STOMP轻松进行消息传递
apollo 參數(shù)傳遞
在我以前的文章中,我介紹了幾個(gè)有趣的用例,這些用例使用著名的消息代理HornetQ和ActiveMQ通過(guò)Websockects實(shí)現(xiàn)STOMP消息傳遞。 但是我沒(méi)有介紹的是Apollo,因?yàn)槲覀€(gè)人認(rèn)為它的API是冗長(zhǎng)的,并且不像Java開(kāi)發(fā)人員那樣表現(xiàn)力強(qiáng)。 盡管如此,我花更多的時(shí)間在Apollo上玩耍,就更加確信我已經(jīng)擁有了巨大的潛力。 所以這篇文章全是關(guān)于阿波羅的 。
我們要解決的問(wèn)題保持不變:簡(jiǎn)單的發(fā)布/訂閱解決方案,其中JavaScript Web客戶端發(fā)送消息并偵聽(tīng)特定主題。 每當(dāng)收到任何消息時(shí),客戶端都會(huì)顯示警報(bào)窗口(請(qǐng)注意,我們需要使用支持Websockets的現(xiàn)代瀏覽器,例如Google Chrome或Mozilla Firefox )。
讓我們從index.html (導(dǎo)入了很棒的stomp.js JavaScript庫(kù))開(kāi)始著手吧 :
<script src="stomp.js"></script><script type="text/javascript">var client = Stomp.client( "ws://localhost:61614/stomp", "v11.stomp" );client.connect( "", "",function() {client.subscribe("/topic/test",function( message ) {alert( message );}, { priority: 9 } );client.send("/topic/test", { priority: 9 }, "Pub/Sub over STOMP!");}); </script>客戶端部分沒(méi)有什么不同,只是主題名稱(chēng)現(xiàn)在為/ topic / test 。 但是服務(wù)器端相差很多。 Apollo是Scala編寫(xiě)的,它包含異步,非阻塞編程模型。 我認(rèn)為,這是一件非常好的事情。 它帶來(lái)的是一個(gè)新的編程范式,也不一定是一件壞事。 AppConfig類(lèi)是用于配置嵌入式Apollo代理的類(lèi):
package com.example.messaging;import java.io.File;import org.apache.activemq.apollo.broker.Broker; import org.apache.activemq.apollo.broker.jmx.dto.JmxDTO; import org.apache.activemq.apollo.dto.AcceptingConnectorDTO; import org.apache.activemq.apollo.dto.BrokerDTO; import org.apache.activemq.apollo.dto.TopicDTO; import org.apache.activemq.apollo.dto.VirtualHostDTO; import org.apache.activemq.apollo.dto.WebAdminDTO; import org.apache.activemq.apollo.stomp.dto.StompDTO; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration public class AppConfig {@Beanpublic Broker broker() throws Exception {final Broker broker = new Broker();// Configure STOMP over WebSockects connectorfinal AcceptingConnectorDTO ws = new AcceptingConnectorDTO();ws.id = "ws";ws.bind = "ws://localhost:61614"; ws.protocols.add( new StompDTO() );// Create a topic with name 'test'final TopicDTO topic = new TopicDTO();topic.id = "test";// Create virtual host (based on localhost)final VirtualHostDTO host = new VirtualHostDTO();host.id = "localhost"; host.topics.add( topic );host.host_names.add( "localhost" );host.host_names.add( "127.0.0.1" );host.auto_create_destinations = false;// Create a web admin UI (REST) accessible at: http://localhost:61680/api/index.html#!/ final WebAdminDTO webadmin = new WebAdminDTO();webadmin.bind = "http://localhost:61680";// Create JMX instrumentation final JmxDTO jmxService = new JmxDTO();jmxService.enabled = true;// Finally, glue all together inside broker configurationfinal BrokerDTO config = new BrokerDTO();config.connectors.add( ws );config.virtual_hosts.add( host );config.web_admins.add( webadmin );config.services.add( jmxService );broker.setConfig( config );broker.setTmp( new File( System.getProperty( "java.io.tmpdir" ) ) );broker.start( new Runnable() { @Overridepublic void run() { System.out.println("The broker has been started started.");}} );return broker;} }我想很清楚我的意思是冗長(zhǎng)且不夠表達(dá),但至少很容易理解。 首先,我們?cè)趙s:// localhost:61614創(chuàng)建Websockects連接器,并要求它支持STOMP協(xié)議。 然后,我們使用名稱(chēng)測(cè)試創(chuàng)建一個(gè)簡(jiǎn)單的主題(在客戶端將其稱(chēng)為/ topic / test )。 下一個(gè)重要步驟是創(chuàng)建虛擬主機(jī),并將主題(和隊(duì)列(如果有)綁定到該主機(jī) )。 主機(jī)名列表非常重要,因?yàn)槟繕?biāo)解析邏輯在很大程度上依賴它。 在接下來(lái)的步驟中,我們將配置Web管理員 UI和JMX工具 ,使我們能夠訪問(wèn)配置,統(tǒng)計(jì)信息和監(jiān)視。 要進(jìn)行檢查,請(qǐng)?jiān)贏pollo代理啟動(dòng)后在Web瀏覽器中打開(kāi)此URL 。 最后,通過(guò)應(yīng)用配置并啟動(dòng)代理,我們一切順利! 如您所見(jiàn),異步編程模型導(dǎo)致回調(diào)和匿名函數(shù)( Java 8在哪里?)。
現(xiàn)在,完成配置后,該看一下放置在Starter類(lèi)中的啟動(dòng)邏輯了(再次,回調(diào)和匿名函數(shù)用于執(zhí)行正常的關(guān)閉邏輯):
package com.example.messaging;import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit;import org.apache.activemq.apollo.broker.Broker; import org.springframework.context.annotation.ConfigurableApplicationContext;public class Starter {public static void main( String[] args ) throws Exception {try( ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( AppConfig.class ) ) {final Broker broker = context.getBean( Broker.class ); System.out.println( "Press any key to terminate ..." );System.in.read(); final CountDownLatch latch = new CountDownLatch( 1 );broker.stop( new Runnable() { @Overridepublic void run() { System.out.println("The broker has been stopped.");latch.countDown();}} );// Gracefully stop the brokerif( !latch.await( 1, TimeUnit.SECONDS ) ) {System.out.println("The broker hasn't been stopped, exiting anyway ...");}}} }與前面的示例一樣 ,在運(yùn)行Starter類(lèi)并在瀏覽器中打開(kāi)index.html之后,我們應(yīng)該看到類(lèi)似以下內(nèi)容:
太好了,效果很好! 我非常確定,只是在Scala中重寫(xiě)代碼,此Apollo API使用示例將看起來(lái)更加緊湊和簡(jiǎn)潔。 無(wú)論如何,如果您正在尋找杰出的消息傳遞體系結(jié)構(gòu),我認(rèn)為Apollo消息代理絕對(duì)值得考慮。
所有資源均可在GitHub: Apollo示例上獲得 。
翻譯自: https://www.javacodegeeks.com/2013/09/easy-messaging-with-stomp-over-websockets-using-apollo.html
apollo 參數(shù)傳遞
總結(jié)
以上是生活随笔為你收集整理的apollo 参数传递_使用Apollo通过WebSocket通过STOMP轻松进行消息传递的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CDN防御(cdn如何防御ddos)
- 下一篇: hook 与aspectj_将Aspec