Sie sind auf Seite 1von 12

ActiveMQ Messaging Services

Using ActiveMQ: Why and When?


ActiveMQ was meant to be used as the JMS spec intended, for remote communications between distributed applications ActiveMQ is used in the environment where applications need to overcome the agenda of tight coupling Asynchronous calls can be made easy in the loosely coupled applications. i.e. Just fire and forget No need to worry about how the application will handle the calls Message is send to MOM() in a one-way fashion

When To Use ActiveMQ Heterogeneous Application Integration ActiveMQ is a tool with cross-language capabilities. Basically it is an ActiveMQ broker is written in java so it basically provides a Java client naturally. But other than that it also provides a multi language client support like c, c++, .NET, Pearl, PHP and many more. Loosen Couple between applications Previously applications with RPC were believed to be very safe as message transmission and its conformation plays a vital role in it. But ActiveMQ change the acceptance of asynchronous scenario i.e. one just need to send the message and need not to worry about it retrieval. This decreases its interdependency and makes it more loosely coupled and flexible. That saves much of time over interdependent systems; Components use fire and forget kind of mechanism. Event Driven Architecture The decoupled asynchronous style of architecture described allows broker itself to manage many clients via tuning, additional memory location and so on (known Vertical Scalability). Instead of rely on adding number of broker nodes relying upon adding and handling the clients.

Features of ActiveMQ
Active MQ is messaging service used for communication over various platforms. ActivceMQ has following features. JMS Compliance

ActiveMQ is an implementation of JMS 1.1 spec. JMS provide guaranties like synchronous and Asynchronous Messaging services, once and once only message delivery, message durability for subscribers and more. Connectivity It provides a wide range of support to protocol like Http/s, Ip multicast, SSL, STOMP, TCP, UDP, XMPP, and more. Support to such wide range protocol increases its usability. Integration with application servers and Secured ActiveMQ is much flexible as it provides a wide integration for application servers. Also for security it provides JAAS login modules and uses property files for authorization and authentication. Various client API It provides a various client support for languages other than Java like c/c++, .NET, Pearl, PHP , Python, Ruby and more. Clustering Many ActiveMQ tools can provide a powerful set of features such as working of many ActiveMQ brokers together that forms federated network of brokers. Dramatically Simplified Administration ActiveMQ after years of monitoring provides a simplified administration tool for control of messages. This administration tool is so simple to look after that no extra resource is allocated for it.

Understanding Message-oriented Middleware


Message Oriented Middleware has played an important role in software communication in asynchronous, loosely-coupled, reliable, scalable and secure manner among distributed applications. MOMs are acting as a message mediator between Message senders and receivers. At high level messages are business information that are transferred from sender to receiver using MOM that are known as destinations. Both sender and receiver need not to get connected at same time. MOM takes the message from sender and sends to relevant receiver based on its destination address based on the availability of that user. This is how MOM helps in achieving loose-coupling. And this is what recommended as asynchronous messaging. Also the beneficiary part of MOM is that it provides a wide support to protocols HTTP/S, multicast, SSL, TCP/IP, UDP and more.

Java Messaging Services (JMS)


Java Messaging services is an API that is developed using Java client but still hides the complexity and supports many other clients for implementations Like .NET, Ruby, Pearl, PHP etc. JMS provided many artifacts in world of messaging like, JMS Client [Pure Java client implementation] NON-JMS Client [App written using Native JMS client] JMS Producer [Client app that creates and sends JMS messages] JMS Consumer [Client app that receives and processes JMS messages] JMS Provider [Implementation interface in Java] JMS Message JMS Domains [P2P and publish/Subscribe] Java Message in JMS is made of 2 following things Headers o o o o o o o o o o Payload o It is just the content of the message the actual content JMSCorrelationID JMSDeliveryMode JMSDestination JMSExpiration JMSMessageID JMSPriority JMSRedelivered JMSReployTo JMSTimestamp JMSType

JMS Message Properties JMSXAppIDIdentifies the application sending the message JMSXConsumerTXIDThe transaction identifier for the transaction within which this message was consumed JMSXDeliveryCountThe number of message delivery attempts JMSXGroupIDThe message group of which this message is a part JMSXGroupSeqThe sequence number of this message within the group JMSXProducerTXIDThe transaction identifier for the transaction within which

this message was produced JMSXRcvTimestampThe time the JMS provider delivered the message to the consumer JMSXStateUsed to define a provider-specific state JMSXUserID Identifies the user sending the message JMS Message Body MessageThe base message type. Used to send a message with no payload, only headers and properties. Typically used for simple event notification. TextMessage A message whose payload is a String. Commonly used to send simple textual and XML data. MapMessage Uses a set of name/value pairs as its payload. The names are of type String and the values are a Java primitive type. BytesMessage Used to contain an array of uninterrupted bytes as the payload. StreamMessageA message with a payload containing a stream of primitive Java types thats filled and read sequentially. ObjectMessageUsed to hold a serializable Java object as its payload. Usually used for complex Java objects. Also supports Java collections. JMS Domains Persistent Non-Persistent

Java Messaging Application Creation Steps


1 Acquire a JMS connection factory 2 Create a JMS connection using the connection factory 3 Start the JMS connection 4 Create a JMS session from the connection 5 Acquire a JMS destination 6 Create a JMS producer, OR a Create a JMS producer b Create a JMS message and address it to a destination 7 Create a JMS consumer a Create a JMS consumer b Optionally register a JMS message listener 8 Send or receive JMS message(s) 9 Close all JMS resources (connection, session, producer, consumer, and so forth)

ActiveMQ Connectors
Understanding Connector URI Its a compact string of characters that are used to refer a resource. Basically every URI has a following string resource <scheme>:<scheme-specific-part> for ex. mailto:abc@activemq.apache.org Where mailto is scheme and Scheme-Specific-Part is the actual mail address. As we saw in previous examples that tcp://localhost:61616?trace=true, ActiveMQ comes with the concept of composite URI, A composite URI automatically reconnects to another URI over failure of first resource URI. Ex static:(tcp://host1:61616,tcp://host2:61616) HERE ABOVE STATIC IS SCHEME AND THERE ARE TWO PATHS Note:- White Space are not supported in composite URI

Transport Connectors
Transport connectors are used to send messages and receive messages from/to broker. Configuring Transport Connectors Let us take that same portfolio example take a look at ActiveMQ demo configuration file (conf/activemq-demo.xml) <transportConnectors> <transport-connector name=openwire uri=tcp://localhost:61616> <transportConnector name="ssl" uri="ssl://localhost:61617"/> <transportConnector name="stomp" uri="stomp://localhost:61613"/> <transportConnector name="xmpp" uri="xmpp://localhost:61222"/> </transportConnectors> Following code should be enough to get on transport connectors ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");

Connection connection = factory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Protocols
TCP [Transmission Control Protocol] TCP has its own benefits of transferring data over peer to peer, over years of long time we are relying on TCP for successfully and securely sending data over Network. In TCP for sending messages in JMS we need to serialize messages in an suitable form. i.e. They must be serialized in and out as byte sequence to be sent over the wire protocol. The default protocol used in ActiveMQ is wire protocol. Syntax: Tcp://hostname:port?key=value&key=value The following snippet of code needs to be added into the default configuration <transportConnectors> <transportConnector name="tcp uri="tcp://localhost:61616?trace=true"/> </transportConnectors> mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch4.Consumer Dexec.args="tcp://localhost:61616 CSCO ORCL"

Benefits of TCP Efficiency: Since TCP uses open wire protocol for converting and receiving bytes over network. It is very efficient in terms of usage and performance. Reliability:

Messages wont be lost over network.

NIO [New I/O Api Protocol] New Input Output connector is not the replacement of traditional Java I/O library, It was an alternative approach to network programming and access to some low-level I/O operations of modern operating systems. In all NIO is same as TCP the only difference is in its implementation TCP uses internally openwire that makes it more secure and is implemented using transport API where as NIO connector is implemented using NIO API When there are so many clients wants to get connected to broker and the system may feel like completely overloaded from clients at that moment NIO protocol can be used Syntax: nio://hostname:port?key=value

Example [Add to the default configuration] i.e \conf\activemq.xml <transportConnectors> <transportConnector name="tcp" uri="tcp://localhost:61616?trace=true" /> <transportConnector name="nio" uri="nio:localhost:61618?trace=true" /> </transportConnectors>

Note: - NIO connector must not set on the client side as it is server specific, client needs to use TCP and since server needs to maintain load and other issues we need to use NIO at server side Lets simply execute the example using following steps: Step 1: Goto /conf/activemq.xml Step 2: Add the Following lines in the <transportconnectors> <transportConnector name="nio" uri="nio:localhost:61618?trace=true" />

Step 3: Restart ActiveMQ Server Step 4: Execute Publisher [mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch4.Publisher \ -Dexec.args="nio://localhost:61618 CSCO ORCL"] Step 5: Execute Consumer [mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch4.Consumer \ -Dexec.args="tcp://localhost:61616 CSCO ORCL"]

Just look at this issue will help a lot in getting the beneath of http://activemq.2283324.n4.nabble.com/Problem-with-NIO-protocol-td3540710.html

UDP [User Datagram Protocol] UDP was designed basically for faster transmission of data over network. Situations where the transferring of data is more important than its reliability in such cases UDP is preferable [Voip, Online Gaming]. Syntax: udp://hostname:port?key=value

Example : Add following code lines to execute this UDP example Edit [/conf/activemq.xml] <transportconnectors> <transportConnector name="udp" uri="udp://localhost:61618?trace=true" /> </transportconnectors>

Lets simply execute the example using following steps: Step 1:

Goto /conf/activemq.xml Step 2: Add the Following lines in the <transportconnectors> <transportConnector name="udp" uri="udp://localhost:61618?trace=true" /> Step 3: Restart ActiveMQ Server Step 4: Execute Publisher [mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch4.Publisher \ -Dexec.args="udp://localhost:61618 CSCO ORCL"] Step 5: Execute Consumer [mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch4.Consumer \ -Dexec.args="tcp://localhost:61616 CSCO ORCL"]

IT completely depends on the user which protocol needs to be used.

SSL [Secure Socket Layer] In situations where security is more concern that transmission SSL is a secure protocol need to be used in such situations. It internally uses the TCP so it covers all the basic TCP properties apart from it, it encrypts the data on TCP channel makes it more secure. It uses SSL transport connector that adds an SSL layer over TCP. It provides an encrypted communication over client and brokers. Syntax: ssl://hostname:port?key=value

Example : Add following code lines to execute this SSL example Edit [/conf/activemq.xml]

<transportConnectors> <transportConnector name="ssl" uri="ssl://localhost:61617?trace=true" /> </transportConnectors>

Here executing example covers some extra steps since we need to create certificates (keystores) for the client. Step 1: Goto /conf/activemq.xml Step 2: Add the Following lines in the <transportconnectors> <transportConnector name="ssl" uri="ssl://localhost:61617?trace=true" /> Step 3: Restart ActiveMQ Server Step 4: Execute Consumer [mvn exec:java -Dexec.mainClass=org.apache.activemq.book.ch4.Consumer \ -Dexec.args="ssl://localhost:61617 CSCO ORCL"] => Gives Error Note : - To resolve this error we need to provide some basic system properties Step 4: Execute Following Command for Publisher mvn -Djavax.net.ssl.keyStore=${ACTIVEMQ_HOME}/conf/client.ks Djavax.net.ssl.keyStorePassword=password Djavax.net.ssl.trustStore=${ACTIVEMQ_HOME}/conf/client.ts exec:java Dexec.mainClass=org.apache.activemq.book.ch4.Publisher -Dexec.args="ssl://localhost:61617 CSCO ORCL" javax.net.ssl.keyStoreDefines which keystore the client should use javax.net.ssl.keyStorePasswordDefines an appropriate password for the keystore javax.net.ssl.trustStoreDefines an appropriate truststore the client should use

Step 5: Execute Following Command for Consumer mvn -Djavax.net.ssl.keyStore=${ACTIVEMQ_HOME}/conf/client.ks Djavax.net.ssl.keyStorePassword=password Djavax.net.ssl.trustStore=${ACTIVEMQ_HOME}/conf/client.ts exec:java Dexec.mainClass=org.apache.activemq.book.ch4.Consumer -Dexec.args="ssl://localhost:61617 CSCO ORCL"

It basically needs two types stored keys and certificates. The first so called is keystore(broker.ks) and the another is truststore (broker.ts)

Note: - You can also enable and disable the SSL ciphers {ACTIVEMQ_HOME}/conf/activemq.xml Add Following Lines of Code <transportConnectors> <transportConnector name="ssl" uri="ssl://localhost:61617?transport.enabledCipherSuites=SSL_RSA_WITH_RC4_128_SHA" /> </transportConnectors>

Hypertext Transfer Protocol [Http/Https] Systems where firewalls are configured to allow basic services such as web access and email. In those scenarios ActiveMQ comes into existence ActiveMQ implements the HTTP Transport connector, which provides the xml formatted data to broker using HTTP Protocol. Syntax: HTTP / HTTPS http://hostname:port?key=value

https://hostname:port?key=value

Example : Add following code lines to execute this SSL example Edit [/conf/activemq.xml] <transportConnectors> <transportConnector name="tcp" uri="tcp://localhost:61616?trace=true"/> <transportConnector name="http" uri="http://localhost:8080?trace=true" /> </transportConnectors> Note:- Basically there are two transports that are listening to port 8080, TCP transport and HTTP Transport. <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-optional</artifactId> <version>5.4.1</version> </dependency>

Das könnte Ihnen auch gefallen