In order to exchange messages, producers and consumers (clients) need to connect tojava
the broker. This client-to-broker communication is performed through transport connectors.apache
ActiveMQ provides an impressive list of protocols clients can use to exchange安全
messages. The requirements of ActiveMQ users in terms of connectivity are diverse.網絡
Some users focus on performance, others on security, and so on. ActiveMQ tries tosession
cover all these aspects and provide a connector for every use case.app
In this section you’ll learn how transport connectors are configured in thetcp
ActiveMQ configuration files and adapt the stock portfolio example to demonstrateide
various connectors. In the following sections, we’ll go through protocols available for性能
connecting to the broker over the network, as well as introduce the concept of the學習
embedded broker and Virtual Machine Protocol used for communicating with brokers
inside your application (a topic that will be continued in chapter 7).
爲了交換消息,消息生產者和消費者(客戶端)須要鏈接到代理(broker).
這種客戶端-代理之間的通訊就是經過傳輸鏈接(transport connector)來完成.
ActiveMQ提供一系列的鏈接協議,客戶端使用這些協議能夠交換消息.
ActiveMQ用戶對鏈接的需求是多種多樣的,好比有些用戶關注性能,有些關注安全等等,
ActiveMQ提供鏈接器以知足全部用戶的需求.
本節中,你將學習如何在ActiveMQ配置文件中配置傳輸鏈接器,同時會修改 stock portfolio例子,
以便闡述各類各樣的連接器.在接下來的章節中,咱們將討論鏈接協議,使用這些鏈接協議,能夠經過網絡鏈接到代理.
另外,本節中還將介紹嵌入式代理(embedded broker)和虛擬機協議,使用虛擬機協議,容許在應用程序內部與代理直接通訊
(這個專題將在第七章繼續討論).
4.2.1 Configuring transport connectors
From the broker’s perspective, the transport connector is a mechanism used to accept
and listen to connections from clients. If you take a look at the ActiveMQ demo configuration
file (conf/activemq-demo.xml), you’ll see the configuration snippet for transport
connectors similar to the following example:
<transportConnectors>
<transportConnector name=」openwire」 uri=」tcp://localhost:61616″ discoveryUri=」multicast://default」/>
<transportConnector name=」ssl」 uri=」ssl://localhost:61617″/>
<transportConnector name=」stomp」 uri=」stomp://localhost:61613″/>
<transportConnector name=」xmpp」 uri=」xmpp://localhost:61222″/>
</transportConnectors>
從代理的(broker)的角度來講,傳輸鏈接(transport connector)是一種機制,用來處理和監聽客戶端的鏈接.
若是你查看ActiveMQ demo的配置文件(conf/activemq-demo.xml),你會看到,傳輸鏈接配置相似於下面的代碼片斷:
<transportConnectors>
<transportConnector name=」openwire」 uri=」tcp://localhost:61616″ discoveryUri=」multicast://default」/>
<transportConnector name=」ssl」 uri=」ssl://localhost:61617″/>
<transportConnector name=」stomp」 uri=」stomp://localhost:61613″/>
<transportConnector name=」xmpp」 uri=」xmpp://localhost:61222″/>
</transportConnectors>
As you can see, transport connectors are defined within the <transportConnectors>
element. You define particular connectors with the appropriate nested <transport-Connector> element.
ActiveMQ simultaneously supports many protocols listening on
different ports. The configuration for a connector must uniquely define the name
and the URI attributes. In this case, the URI defines the network protocol and optional
parameters through which ActiveMQ will be exposed for connectivity. The
discoveryUri attribute as shown on the OpenWire connector is optional and will be
discussed further in section 4.3.1.
正如你看到的,傳輸鏈接使用<transportConnectors>元素來定義.經過嵌套的<transport-Connector>元素
來定義特定的鏈接.ActiveMQ支持同時支持不一樣的鏈接協議監聽不一樣的端口.鏈接器配置的name和URI屬性必須惟一.
這樣,經過URI定義網絡協議以及可選的參數,ActiveMQ便產生了一個鏈接.在OpenWire connector配置中的
discoveryUri屬相是可選的,這部分將在4.3.1節中討論.
The preceding snippet defines four transport connectors. Upon starting up
ActiveMQ using such a configuration file, you’ll see the following log in the console as
these connectors start up:
INFO TransportServerThreadSupport – Listening for connections at:tcp://localhost:61616
INFO TransportConnector – Connector openwire Started
INFO TransportServerThreadSupport – Listening for connections at:ssl://localhost:61617
INFO TransportConnector – Connector ssl Started
INFO TransportServerThreadSupport – Listening for connections at:stomp://localhost:61613
INFO TransportConnector – Connector stomp Started
INFO TransportServerThreadSupport – Listening for connections at:xmpp://localhost:61222
INFO TransportConnector – Connector xmpp Started
前面的代碼片段中,一共定義了4個傳輸鏈接.使用包含上面代碼片斷的配置文件啓動ActiveMQ時,控制檯
會顯示下面的日誌信息:
INFO TransportServerThreadSupport – Listening for connections at:tcp://localhost:61616
INFO TransportConnector – Connector openwire Started
INFO TransportServerThreadSupport – Listening for connections at:ssl://localhost:61617
INFO TransportConnector – Connector ssl Started
INFO TransportServerThreadSupport – Listening for connections at:stomp://localhost:61613
INFO TransportConnector – Connector stomp Started
INFO TransportServerThreadSupport – Listening for connections at:xmpp://localhost:61222
INFO TransportConnector – Connector xmpp Started
From the client’s perspective, the transport connector URI is used to create a connection
to the broker in order to send and receive messages. Sending and receiving messages
will be discussed in detail in chapter 7, but the following code snippet should be
enough to demonstrate the usage of the transport connector URIs in Java applications:
從客戶端的角度來講,使用傳輸鏈接的URI能夠建立一個到代理的鏈接,以便接收和發送消息.
接收和發送消息將在第7章詳細討論,下面的代碼片斷足以說明Java應用程序中傳輸鏈接URI
(transport connector URI)的用處.
ActiveMQConnectionFactory factory =
new ActiveMQConnectionFactory(「tcp://localhost:61616″);
Connection connection = factory.createConnection();
connection.start();
Session session =
connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Note in the preceding example that the transport connector URIs defined in
ActiveMQ configuration are used by the client application to create a connection to
the broker. In this case, the URI for the TCP transport is used and is shown in bold
text.
注意,前面的例子中,客戶端程序使用ActiveMQ配置文件中定義的的傳輸鏈接器的URI來建立到代理的鏈接.
代碼中,展現瞭如何使用TCP transport的URI並以斜體顯示.
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(「tcp://localhost:61616″);
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
NOTE The important thing to know is that we can use the query part of the
URI to configure connection parameters both on the server and client sides.
Usually most of the parameters apply both for client and server sides of the
connection, but some of them are specific to one or the other, so be sure you
check the protocol reference before using the particular query parameter.
With this basic understanding of configuring transport connectors, it’s important to
become aware of and understand the available transport connectors in ActiveMQ. But
before we start explaining particular connectors, we must first adapt our stock portfolio
example so it can be used with different transport connectors.
注意:須要重點了解的是:咱們可使用URI中查詢字符串部分的來配置服務端和客戶端的鏈接參數.
一般這些參數同時適用於服務端和客戶端,可是也有一些僅適用於其中之一,因此在使用查詢字符串以前
須要查看鏈接協議的使用手冊.有了這些配置傳輸鏈接的基本知識後,須要重點了解ActiveMQ提供了哪些可用傳輸鏈接了.
但在解釋這些鏈接器以前,咱們首先修改下stock portfolio的例子,這樣能夠先熟悉不一樣的傳輸鏈接.
4.2.2 Adapting the stock portfolio example
Chapter 3 introduced a stock portfolio example that uses ActiveMQ to publish and
consume stock exchange data. There, we used the fixed standard connector URI since
we wanted to make those introductory examples as simple as possible. In this chapter,
we’ll explain all protocols and demonstrate them by running the stock portfolio
example using each of them. For that reason, we need to modify the stock portfolio
example so it will work using any of the protocols.
Listing 4.1 is a modified version of the main() method from the stock portfolio
publisher.
第三章介紹了一個stock portfolio實例,在這個實例中使用ActiveMQ發送和接收處理消息,以便進行股票價格信息交換.
爲了使stock portfolio這個入門性的實例看起來儘可能簡單,咱們使用了固定的標準的URI配置鏈接器.
本章將解釋全部鏈接協議,並在stock portfolio這個例子中使用各類鏈接協議.爲此,咱們須要修改
stock portfolio例子,使得這個例子能夠在使用任何鏈接實例時可以正常運行.
清單4.1是stock portfolio例子中修改後的Publisher類的main()方法.
Listing 4.1 Modifying stock portfolio publisher to support various connector URIs
public static void main(String[] args) throws JMSException
{
if (args.length == 0)
{
System.err.println(「Please define a connection URI!」);
return;
}
Publisher publisher = new Publisher(args[0]);
String[] topics = new String[args.length – 1];
System.arraycopy(args, 1, topics, 0, args.length – 1);
while (total < 1000)
{
for (int i = 0; i < count; i++)
{
publisher.sendMessage(topics);
}
total += count;
System.out.println(「Published ‘」 + count + 「‘ of ‘」 + total + 「‘ price messages」);
try
{
Thread.sleep(1000);
}
catch (InterruptedException x)
{
}
}
publisher.close();
}
The preceding code ensures that the connector URI is passed as the first argument
and extracts topic names from the rest of the arguments passed to the application.
Now the stock portfolio publisher can be run with the following command:
前面的代碼保證了程序運行時第一個命令行參數是鏈接器的URI,剩下的參數會被提取出來做爲主題的名稱.
至此可有下面的命令執行那個stock portfolio實例中的publisher.
$ mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch4.Publisher -Dexec.args=」tcp://localhost:61616 CSCO ORCL」
…
Sending: {price=65.713356601409, stock=JAVA, offer=65.779069958011,up=true}on destination: topic://STOCKS.JAVA
Sending: {price=66.071605671946, stock=JAVA, offer=66.137677277617,up=true}on destination: topic://STOCKS.JAVA
Sending: {price=65.929035001620, stock=JAVA, offer=65.994964036622,up=false}on destination: topic://STOCKS.JAVA
…
Note that one more argument has been added to the publisher: the URL to be used to
connect to the appropriate broker.
注意在執行publisher類時,新增了一個URI參數鏈接器使用這個參數鏈接到合適的代理.
The same principle can be used to modify the stock portfolio consumer. In the following
listing, you’ll find the stock portfolio consumer’s main() method modified to
accept the connection URI as a first parameter.
使用一樣的方法,能夠修改consumer類.在下面的代碼清單中,你會看到stock portfolio實例的
consumer類的main()方法已經被修改過了以便使用第一個命令行參數做爲鏈接器的URI.
Listing 4.2 Modifying stock portfolio consumer to support various connector URIs
public static void main(String[] args) throws JMSException
{
if (args.length == 0)
{
System.err.println(「Please define connection URI!」);
return;
}
Consumer consumer = new Consumer(args[0]);
String[] topics = new String[args.length – 1];
System.arraycopy(args, 1, topics, 0, args.length – 1);
for (String stock : topics)
{
Destination destination = consumer.getSession().createTopic(「STOCKS.」 + stock);
MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination);
messageConsumer.setMessageListener(new Listener());
}
}
In order to achieve the same functionality as in the chapter 3 example, you should
run the consumer with an extra URI argument.
The following example shows how to do this:
下面的命令展現了執行consumer的命令行命令,改命令使用了一個額外的URI參數,以便使得這裏的consumer
類與第三章中的consumer類功能相同.
$ mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch4.Consumer -Dexec.args=」tcp://localhost:61616 CSCO ORCL」
…
ORCL 65.71 65.78 up
ORCL 66.07 66.14 up
ORCL 65.93 65.99 down
CSCO 23.30 23.33 up
…
Note that the message flow between the producer and the consumer is the same as in
the original example. With these changes, the examples are now ready to be run using
a variety of supported protocols. Let’s now dig into the particular transport connectors.
In the following section we’ll see what options you have if you want to connect to
the broker over the network.
注意,修改後例子裏面producer和consumer這個之間的消息流與未修改以前是相同的.作完這些修改後,
這個例子能夠支持多種鏈接協議了.如今讓咱們深刻探討各類傳輸鏈接.在接下來的章節中,能夠看到使用哪些
方法能夠鏈接到代理.