欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
ActiveMQ學(xué)習系列(二)----生產(chǎn)者客戶(hù)端(java)

上文主要簡(jiǎn)單地將activeMq搭建了起來(lái),并且可以用web console去登錄查看相關(guān)的后臺功能。

本文將學(xué)習如何用java語(yǔ)言實(shí)現一個(gè)生產(chǎn)者客戶(hù)端,主要參考了以下鏈接:

http://activemq.apache.org/jndi-support.html

代碼已上傳github,建議先下載下來(lái)實(shí)際運行一遍:

https://github.com/cctvckl/big-data-learning/tree/master/activemq-learning

 

一、ActiveMq支持的協(xié)議

ActiveMq作為消息中間件,支持多種連接協(xié)議,如:tcp、amqp、stomp、mqtt等。

如果啟動(dòng)時(shí)以./activemq console方式啟動(dòng),可以看到如下輸出:

而下文將要講解的java客戶(hù)端程序,就是基于其中的tcp協(xié)議。

將tcp://host:port這個(gè)地址記錄下來(lái),下面需要用到。

二、大體思路

1、本地配置文件,配置要連接的ActiveMq服務(wù)器、包括連接協(xié)議和端口號,配置要發(fā)送消息的目標隊列、目標topic等等。

2、程序讀取上述配置文件,生成連接會(huì )話(huà)、生成消息生產(chǎn)者、發(fā)送消息、關(guān)閉連接。
三、具體步驟

1、配置文件樣例:

java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory# Use the following property to configure the default connectorjava.naming.provider.url = tcp://192.168.2.140:61616# Use the following property to specify the JNDI name the connection factory# should appear as.connectionFactoryNames = ConnectionFactory, queueConnectionFactory, topicConnectionFactry# Register some queues in JNDI using the form:#   queue.[jndiName] = [physicalName]queue.MyQueue = example.MyQueue# Register some topics in JNDI using the form:#   topic.[jndiName] = [physicalName]topic.MyTopic = example.MyTopic

釋義:上面的url項要與第一章節里面的那個(gè)地址匹配;

topic.MyTopic中的點(diǎn)號分割開(kāi)的第二部分(此例為MyTopic)會(huì )被注冊為JNDI名, 至于value(example.MyTopic)為topic名,在Web Console可以看到。

 

queue.MyQueue同理。

 

2、配置文件完畢,下面介紹業(yè)務(wù)代碼:

package com.ckl.activemq;/** * The SimpleQueueSender class consists only of a main method, * which sends several messages to a queue. * * Run this program in conjunction with SimpleQueueReceiver. * Specify a queue name on the command line when you run the * program.  By default, the program sends one message.  Specify * a number after the queue name to send that number of messages. */import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.jms.*;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;/** * A simple polymorphic JMS producer which can work with Queues or Topics which * uses JNDI to lookup the JMS connection factory and destination. */public class SimpleProducer {    private static final Logger LOG = LoggerFactory.getLogger(SimpleProducer.class);    private SimpleProducer() {}    /**     * @param args the destination name to send to and optionally, the number of     *                messages to send     */    public static void main(String[] args) {        Context jndiContext = null;        ConnectionFactory connectionFactory = null;        Connection connection = null;        Session session = null;        Destination destination = null;        MessageProducer producer = null;        String destinationName = null;        final int numMsgs;
    //這邊被我手動(dòng)修改了,比較不喜歡每次運行時(shí)還要修改Run configuration,麻煩。 args = new String[2]; args[0] = "MyTopic"; args[1] = "3"; if ((args.length < 1) || (args.length > 2)) { LOG.info("Usage: java SimpleProducer <destination-name> [<number-of-messages>]"); System.exit(1); } destinationName = args[0]; LOG.info("Destination name is " + destinationName); if (args.length == 2) { numMsgs = (new Integer(args[1])).intValue(); } else { numMsgs = 1; } /* * Create a JNDI API InitialContext object */ try { jndiContext = new InitialContext(); } catch (NamingException e) { LOG.info("Could not create JNDI API context: " + e.toString()); System.exit(1); } /* * Look up connection factory and destination. */ try { connectionFactory = (ConnectionFactory)jndiContext.lookup("ConnectionFactory"); destination = (Destination)jndiContext.lookup(destinationName); } catch (NamingException e) { LOG.info("JNDI API lookup failed: " + e); System.exit(1); } /* * Create connection. Create session from connection; false means * session is not transacted. Create sender and text message. Send * messages, varying text slightly. Send end-of-messages message. * Finally, close the connection. */ try { connection = connectionFactory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); producer = session.createProducer(destination); TextMessage message = session.createTextMessage(); for (int i = 0; i < numMsgs; i++) { message.setText("This is message " + (i + 1)); LOG.info("Sending message: " + message.getText()); producer.send(message); } /* * Send a non-text control message indicating end of messages. */ producer.send(session.createMessage()); } catch (JMSException e) { LOG.info("Exception occurred: " + e); } finally {
       //睡眠是我手動(dòng)加的,主要為了觀(guān)察效果 try { Thread.sleep(100000L); } catch (InterruptedException e) { e.printStackTrace(); } if (connection != null) { try { connection.close(); } catch (JMSException ignored) {} } } }}

代碼不難理解:jndi讀取配置文件,建立連接,發(fā)消息,關(guān)閉連接。

運行結果:

 

此時(shí)查看Web Console,

可以看到來(lái)自客戶(hù)端的連接信息。

 

本例子就先到這里,詳細還請參考貼的代碼鏈接和官網(wǎng)文檔。

歡迎留言交流。

 

如果,您認為閱讀這篇博客讓您有些收獲,不妨點(diǎn)擊一下右下角的【推薦】。

如果,您希望更容易地發(fā)現我的新博客,不妨點(diǎn)擊一下,【關(guān)注我

博文是自己對學(xué)習成果的總結,學(xué)習總結知識-》分析問(wèn)題-》解決問(wèn)題。

文中存在的觀(guān)點(diǎn)/描述不正確的地方,歡迎指正。

感謝您的閱讀,如果您對我的博客所講述的內容有興趣,請繼續關(guān)注我的后續博客,我是逐日,qq:914000408。

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Mule : Configuring Jms
JMS入門(mén)之實(shí)例
【Active入門(mén)
activeMQ學(xué)習過(guò)程
ActiveMQ_部署及發(fā)送接收消息
activeMQ跟Jetty集成
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久