`

4.2 传输连接器

 
阅读更多

4.2 Transport connectors

4.2 传输连接器

 

  In order to exchange messages, producers and consumers (clients) need to connect to

the broker. This client-to-broker communication is performed through transport connectors.

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 to

cover all these aspects and provide a connector for every use case.

 

  In this section you’ll learn how transport connectors are configured in the

ActiveMQ configuration files and adapt the stock portfolio example to demonstrate

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这个之间的消息流与未修改之前是相同的.做完这些修改后,

这个例子可以支持多种连接协议了.现在让我们深入探讨各种传输连接.在接下来的章节中,可以看到使用哪些

方法可以连接到代理.

分享到:
评论

相关推荐

    黑莓桌面管理器4.2

    **黑莓桌面管理器4.2详解** 黑莓桌面管理器4.2是一款专为Blackberry OS 5.0及以下版本设计的管理工具,它为用户提供了与黑莓手机进行高效交互的平台。这款软件以其简洁的界面和易于操作的特点,深受黑莓用户的喜爱...

    各种常见PCB直插连接器

    13. 5556-2P和L6.2-4P对接连接器:4.2mm和6.2mm间距的对插连接器,适用于多种电压和电流条件,具有良好的绝缘和接触电阻性能。 14. CF-3P条形连接器:6.35mm间距的连接器,适用于需要较大间距和稳定性的应用。 在...

    蓝牙4.2协议 Core_v4.2

    蓝牙4.2协议是在2014年12月发布,是当时最新的一代蓝牙技术标准,该版本提供了更好的数据传输速率、隐私保护以及与互联网的更好连接能力。 在提到蓝牙4.2核心协议时,核心指的是蓝牙技术核心规范(Bluetooth Core ...

    BLE_4.2核心规范

    蓝牙4.2的协议栈包括物理层(PHY)、链路层(LL)、主机控制器接口(HCI)、逻辑链路控制与适配协议(L2CAP)、属性协议(ATT)和GATT,以及应用层。每一层都有其特定的功能和责任,共同保证了蓝牙设备间的高效、...

    MDB协议 4.2中文完整版

    - **连接器规格**: 明确了用于连接设备的物理接口的类型和规格。 - **示例原理图**: 提供了实现硬件连接的具体示例。 ##### 5. 硬币接受者/变换者 - **VMC/外围通信规范**: 描述了自动售货机控制单元 (VMC) 与硬币...

    蓝牙内核协议_v4.2_蓝牙4.2协议_蓝牙内核_蓝牙协议_

    在《蓝牙内核协议_v4.2.pdf》这份文档中,读者可以深入理解蓝牙4.2的规范细节,包括物理层(PHY)、链路层(LINK Layer)、主机控制器接口(HCI)、逻辑链路控制和适应协议(L2CAP)、属性协议(GATT)以及安全管理(SMP)等核心...

    连接器、线缆选型及其组件设计规范...pdf

    4.2章节详细讨论了欧式连接器,首先概述了其特性,如体积、接插方式、额定电流和电压等,然后给出了选型步骤和考虑因素,包括环境条件、机械寿命、电磁兼容性等。4.3章节对2MM连接器进行了介绍,包括其特性(如小型...

    IAR EW430 V4.2版本下的仿真器驱动

    仿真器驱动在嵌入式开发中起着至关重要的作用,它是连接计算机与目标硬件的桥梁,使得开发者能够在PC上对MSP430微控制器进行程序的编译、下载和调试。在IAR EW4.2环境下,驱动程序通常包含了必要的USB驱动和通信协议...

    EduKit2410下Wince4.2系统映像下载说明

    - 将实验子板S3C2410插到主板连接器上,并进行必要的跳线设置。 - 连接仿真器到实验平台。 - **编译与烧写过程**: - 使用EmbestIDE打开NBOOT_EDUKIT2410.pjf工程文件,并编译生成EduKit2410_nboot.bin文件。 -...

    传输管理器1

    类型定义进一步扩展了基础类型,例如定义特定的结构体或类,以适应传输管理器的特定需求,如文件传输任务对象、网络连接状态对象等。 综上所述,传输管理器1是一个综合性的网络数据传输工具,其设计涵盖了数据处理...

    sysmex_希森美康的Laboman4.2模拟仪器串口通讯的安装说明

    安装Laboman4.2后,数据流将通过TCP/IP协议进行传输,这种方式避免了传统串口连接时可能遇到的问题,并且减少了对于LIS工程师的技术依赖。 #### 三、安装步骤详解 **1. 修改IPU数据传输模式** 首先,需要从IPU...

    Bluetooth 4.2 Quick Reference Guide.pdf

    蓝牙4.2在传输速度上也有了明显的提升,数据吞吐量可以增加多达2.5倍,且增加了10倍的数据包容量。 - LE数据长度扩展:蓝牙4.2版的数据长度扩展功能使得空中固件更新可以比先前版本快250%且更加可靠。 综上所述,...

    onfi4.2 协议文档英文版

    文档的目的是为NAND闪存接口提供一个标准化的连接器设计,这有助于制造商在设计支持NAND闪存的硬件时保持兼容性,并能够利用最新的技术进步。在协议中,详细规定了连接器的物理特性、电气规范、信号定义以及操作模式...

    蓝牙4.2:不用WiFi的连接技术.docx

    ### 蓝牙4.2:不用WiFi的连接技术 #### 概述 随着无线通信技术的不断发展,蓝牙和WiFi已成为人们日常生活中不可或缺的一部分。在众多无线连接技术中,蓝牙和WiFi尤为突出。然而,当WiFi出现故障时,依赖于它的智能...

    use sse in spring4.2

    这种技术允许服务器初始化数据传输给客户端,一旦初始客户端连接建立后即可实现。SSE常用于向浏览器客户端发送消息更新或连续数据流,并且是通过一个名为EventSource的JavaScript API来实现的。客户端通过请求特定的...

    Bluetooth SIG官方协议,Bluetooth 4.2,BLE 4.2,非扫描版本。

    综上所述,Bluetooth 4.2(包括BLE 4.2)的发布带来了许多重要的改进和技术革新,特别是在数据传输速度、隐私保护、连接范围和稳定性方面。这些改进不仅提升了用户体验,也为开发人员提供了更多可能性,使得蓝牙技术...

    spring4.2+hibernate4.2+struts2.3.29整合所需jar包

    - **mysql-connector-java-5.1.31-bin.jar**:这是MySQL的Java连接器,用于连接Java应用程序到MySQL数据库。 - **jackson-mapper-asl-1.9.2.jar**:Jackson 是一个JSON处理器,用于在Java对象和JSON之间进行序列化和...

    CA-SDKv4.2(CA210 驱动)

    CA-SDKv4.2是针对CA210彩色分析仪设计的一款驱动程序,主要用于帮助用户将该设备成功地与计算机进行连接并实现数据交互。CA210是一款专业级的色彩测量工具,广泛应用于印刷、电子、纺织、化妆品等行业,以确保产品...

    Core1553BRM v4.2 Handbook

    6. **外部组件**:这部分介绍了与Core1553BRM接口的外部组件,如物理连接器、电缆和终端电阻,以及它们对总线性能的影响。 7. **MIL-STD-1553B总线概述**:这部分深入解释了MIL-STD-1553B总线的工作原理,包括其双...

Global site tag (gtag.js) - Google Analytics