Chapter 9. Examples

The JBoss Messaging distribution comes with a wide variety of run out-of-the-box examples demonstrating many of the features.

The examples are available in the distribution, in the examples directory. Examples are split into JMS and core examples. JMS examples show how a particular feature can be used by a normal JMS client. Core examples show how the equivalent feature can be used by a core messaging client.

A set of Java EE examples are also provided which need the JBoss Application Server installed to be able to run.

9.1. JMS Examples

To run a JMS example, simply cd into the appropriate example directory and type ant.

You will need to have Apache 1.7.0 or later ant installed on your system with the ant bin directory on your path.

Here's a listing of the examples with a brief description.

9.1.1. Application-Layer Failover

JBoss Messaging implements fully transparent automatic failover of connections from a live to backup node, this requires no special coding for failover, and is described in a different example. Automatic failover requires server replication.

However, JBoss Messaging also supports Application-Layer failover, useful in the case that replication is not enabled on the server side.

With Application-Layer failover, it's up to the application to register a JMS ExceptionListener with JBoss Messaging which will be called by JBoss Messaging in the event that connection failure is detected.

The code in the ExceptionListener then recreates the JMS connection, session, etc on another node and the application can continue.

Application-layer failover is an alternative approach to High Availability (HA). Application-layer failover differs from automatic failover in that some client side coding is required in order to implement this. Also, with Application-layer failover, since the old session object dies and a new one is created, any uncommitted work in the old session will be lost, and any unacknowledged messages might be redelivered.

9.1.2. Automatic (Transparent) Failover

The automatic-failover example demonstrates two servers coupled as a live-backup pair for high availability (HA), and a client connection transparently failing over from live to backup when the live server is crashed.

JBoss Messaging implements seamless, transparent failover of client connections between live and backup servers. This is implemented by the replication of state between live and backup nodes. When replication is configured and a live node crashes, the client connections can carry on as if nothing happened and carry on sending and consuming messages.

9.1.3. Automatic Reconnect

The reconnect-same-node example demonstrates how JBoss Messaging connections can be configured to be resilient to temporary network failures.

In the case of a network failure being detected, either as a result of a failure to read/write to the connection, or the failure of a pong to arrive back from the server in good time after a ping is sent, instead of failing the connection immediately and notifying any user ExceptionListener objects, JBoss Messaging can be configured to automatically retry the connection, and reconnect to the server when it becomes available again across the network.

9.1.4. Browser

The browser example shows you how to use a JMS QueueBrowser with JBoss Messaging.

Queues are a standard part of JMS, please consult the JMS 1.1 specification for full details.

A QueueBrowser is used to look at messages on the queue without removing them. It can scan the entire content of a queue or only messages matching a message selector.

9.1.5. Core Bridge Example

The bridge example demonstrates a core bridge deployed on one server, which consumes messages from a local queue and forwards them to an address on a second server.

Core bridges are used to create message flows between any two JBoss Messaging servers which are remotely separated. Core bridges are resilient and will cope with temporary connection failure allowing them to be an ideal choice for forwarding over unreliable connections, e.g. a WAN.

9.1.6. Client Kickoff

The client-kickoff example shows how to terminate client connections given an IP address using the JMX management API.

9.1.7. Client Side Load-Balancing

The client-side-load-balancing example demonstrates how subsequent connections created from a JMS ConnectionFactory can be created to different nodes of the cluster. In other words it demonstrates how JBoss Messaging does client side load balancing of connections across the cluster.

9.1.8. Clustered Topic

The clustered-topic example demonstrates a JMS topic deployed on two different nodes. The two nodes are configured to form a cluster. We then create a subscriber on the topic on each node, and we create a producer on only one of the nodes. We then send some messages via the producer, and we verify that both subscribers receive all the sent messages.

9.1.9. Dead Letter

The dead-letter example shows you how to define and deal with dead letter messages. Messages can be delivered unsuccessfully (e.g. if the transacted session used to consume them is rolled back).

Such a message goes back to the JMS destination ready to be redelivered. However, this means it is possible for a message to be delivered again and again without any success and remain in the destination, clogging the system.

To prevent this, messaging systems define dead letter messages: after a specified unsuccessful delivery attempts, the message is removed from the destination and put instead in a dead letter destination where they can be consumed for further investigation.

9.1.10. Delayed Redelivery

The delayed-redelivery example demonstrates how JBoss Messaging can be configured to provide a delayed redelivery in the case a message needs to be redelivered.

Delaying redelivery can often be useful in the case that clients regularly fail or roll-back. Without a delayed redelivery, the system can get into a "thrashing" state, with delivery being attempted, the client rolling back, and delivery being re-attempted in quick succession, using up valuable CPU and network resources.

9.1.11. Divert

JBoss Messaging diverts allow messages to be transparently "diverted" from one address to another with just some simple configuration defined on the server side.

9.1.12. Durable Subscription

The durable-subscription example shows you how to use a durable subscription with JBoss Messaging. Durable subscriptions are a standard part of JMS, please consult the JMS 1.1 specification for full details.

Unlike non-durable subscriptions, the key function of durable subscriptions is that the messages contained in them persist longer than the lifetime of the subscriber - i.e. they will accumulate messages sent to the topic even if there is no active subscriber on them. They will also survive server restarts. Note that for the messages to be persisted, the messages sent to them must be marked as persistent messages.

9.1.13. Embedded

The embedded example shows how to embed the JBoss Messaging Server within your own code.

9.1.14. HTTP Transport

The http-transport example shows you how to configure JBoss Messaging to use the HTTP protocol as its transport layer.

9.1.15. Instantiate JMS Objects Directly

Usually, JMS Objects such as ConnectionFactory, Queue and Topic instances are looked up from JNDI before being used by the client code. This objects are called "administered objects" in JMS terminology.

However, in some cases a JNDI server may not be available or desired. To come to the rescue JBoss Messaging also supports the direct instantiation of these administered objects on the client side so you don't have to use JNDI for JMS.

9.1.16. Interceptor

JBoss Messaging allows an application to use an interceptor to hook into the messaging system. Interceptors allow you to handle various message events in JBoss Messaging.

9.1.17. JAAS

The jaas example shows you how to configure JBoss Messaging to use JAAS for security. JBoss Messaging can leverage JAAS to delegate user authentication and authorization to existing security infrastructure.

9.1.18. JMX Management

The jmx example shows how to manage JBoss Messaging using JMX.

9.1.19. Large Message

The large-message example shows you how to send and receive very large messages with JBoss Messaging. JBoss Messaging supports the sending and receiving of huge messages, much larger than can fit in available RAM on the client or server. Effectively the only limit to message size is the amount of disk space you have on the server.

Large messages are persisted on the server so they can survive a server restart. In other words JBoss Messaging doesn't just do a simple socket stream from the sender to the consumer.

9.1.20. Last-Value Queue

The last-value-queue example shows you how to define and deal with last-value queues. Last-Value queues are special queues which discard any messages when a newer message with the same value for a well-defined last-value property is put in the queue. In other words, a last-value queue only retains the last value.

A typical example for last-value queue is for stock prices, where you are only interested by the latest value for a particular stock.

9.1.21. Load Balanced Clustered Queue

The clustered-queue example demonstrates a JMS queue deployed on two different nodes. The two nodes are configured to form a cluster.

We then create a consumer on the queue on each node, and we create a producer on only one of the nodes. We then send some messages via the producer, and we verify that both consumers receive the sent messages in a round-robin fashion.

In other words, JBoss Messaging load balances the sent messages across all consumers on the cluster

9.1.22. Management

The management example shows how to manage JBoss Messaging using JMS Messages to invoke management operations on the server.

9.1.23. Management Notification

The management-notification example shows how to receive management notifications from JBoss Messaging using JMS messages. JBoss Messaging servers emit management notifications when events of interest occur (consumers are created or closed, destinations are created or deleted, security authentication fails, etc.).

9.1.24. Message Consumer Rate Limiting

With JBoss Messaging you can specify a maximum consume rate at which a JMS MessageConsumer will consume messages. This can be specified when creating or deploying the connection factory.

If this value is specified then JBoss Messaging will ensure that messages are never consumed at a rate higher than the specified rate. This is a form of consumer throttling.

9.1.25. Message Counter

The message-counters example shows you how to use message counters to obtain message information for a JMS queue.

9.1.26. Message Expiration

The expiry example shows you how to define and deal with message expiration. Messages can be retained in the messaging system for a limited period of time before being removed. JMS specification states that clients should not receive messages that have been expired (but it does not guarantee this will not happen).

JBoss Messaging can assign an expiry destination to a given queue so that when messages are expired, they are removed from the queue and sent to the expiry destination. These "expired" messages can later be consumed from the expiry destination for further inspection.

9.1.27. Message Group

The message-group example shows you how to configure and use message groups with JBoss Messaging. Message groups allow you to pin messages so they are only consumed by a single consumer. Message groups are sets of messages that has the following characteristics:

  • Messages in a message group share the same group id, i.e. they have same JMSXGroupID string property values

  • The consumer that receives the first message of a group will receive all the messages that belongs to the group

9.1.28. Message Producer Rate Limiting

The producer-rte-limit example demonstrates how, with JBoss Messaging, you can specify a maximum send rate at which a JMS message producer will send messages.

9.1.29. Message Priority

Message Priority carries the delivery preference of messages.

It can be retrieved by the message's standard header field 'JMSPriority' as defined in JMS specification version 1.1.

The value is of type integer, ranging from 0 (the lowest) to 9 (the highest). When messages are being delivered, their priorities will effect their order of delivery. Messages of higher priorities will likely be delivered before those of lower priorities.

Messages of equal priorities are delivered in the natural order of their arrival at their destinations. Please consult the JMS 1.1 specification for full details.

9.1.30. Message Redistribution

The queue-message-redistribution example demonstrates message redistribution between queues with the same name deployed in different nodes of a cluster.

9.1.31. No Consumer Buffering

By default, JBoss Messaging consumers buffer messages from the server in a client side buffer before you actually receive them on the client side. This improves performance since otherwise every time you called receive() or had processed the last message in a MessageListener onMessage() method, the JBoss Messaging client would have to go the server to request the next message, which would then get sent to the client side, if one was available.

This would involve a network round trip for every message and really reduce performance. Therefore, by default, JBoss Messaging pre-fetches messages into a buffer on each consumer.

In some case buffering is not desirable, and JBoss Messaging allows it to be switched off. This example demonstrates that.

9.1.32. Paging

The paging example shows how JBoss Messaging can support huge queues even when the server is running in limited RAM. It does this by transparently paging messages to disk, and depaging them when they are required.

9.1.33. Pre-Acknowledge

Standard JMS supports three acknowledgement modes: AUTO_ACKNOWLEDGE, CLIENT_ACKNOWLEDGE, and DUPS_OK_ACKNOWLEDGE. For a full description on these modes please consult the JMS specification, or any JMS tutorial.

All of these standard modes involve sending acknowledgements from the client to the server. However in some cases, you really don't mind losing messages in event of failure, so it would make sense to acknowledge the message on the server before delivering it to the client. This example demonstrates how JBoss Messaging allows this with an extra acknowledgement mode.

9.1.34. Queue

A simple example demonstrating a JMS queue.

9.1.35. Queue Requestor

A simple example demonstrating a JMS queue requestor.

9.1.36. Queue with Message Selector

The queue-selector example shows you how to selectively consume messages using message selectors with queue consumers.

9.1.37. Request-Response

A simple example showing the JMS request-response pattern.

9.1.38. Scheduled Message

The scheduled-message example shows you how to send a scheduled message to a JMS Queue with JBoss Messaging. Scheduled messages won't get delivered until a specified time in the future.

9.1.39. Security

The security example shows you how configure and use role based queue security with JBoss Messaging.

9.1.40. Send Acknowledgements

The send-acknowledgements example shows you how to use JBoss Messaging's advanced asynchronous send acknowledgements feature to obtain acknowledgement from the server that sends have been received and processed in a separate stream to the sent messages.

9.1.41. Static Message Selector

The static-selector example shows you how to configure a JBoss Messaging core queue with static message selectors (filters).

9.1.42. Static Message Selector Using JMS

The static-selector-jms example shows you how to configure a JBoss Messaging queue with static message selectors (filters) using JMS.

9.1.43. SSL Transport

The ssl-enabled shows you how to configure SSL with JBoss Messaging to send and receive message.

9.1.44. Symmetric Cluster

The symmetric-cluster example demonstrates a symmetric cluster set-up with JBoss Messaging.

JBoss Messaging has extremely flexible clustering which allows you to set-up servers in many different topologies. The most common topology that you'll perhaps be familiar with if you are used to application server clustering is a symmetric cluster.

With a symmetric cluster, the cluster is homogeneous, i.e. each node is configured the same as every other node, and every node is connected to every other node in the cluster.

9.1.45. Temporary Queue

A simple example demonstrating how to use a JMS temporary queue.

9.1.46. Topic

A simple example demonstrating a JMS topic.

9.1.47. Topic Hierarchy

JBoss Messaging supports topic hierarchies. With a topic hierarchy you can register a subscriber with a wild-card and that subscriber will receive any messages sent to an address that matches the wild card.

9.1.48. Topic Selector 1

The topic-selector-example1 example shows you how to send message to a JMS Topic, and subscribe them using selectors with JBoss Messaging.

9.1.49. Topic Selector 2

The topic-selector-example1 example shows you how to selectively consume messages using message selectors with topic consumers.

9.1.50. Transactional Session

The transactional example shows you how to use a transactional Session with JBoss Messaging.

9.1.51. XA Heuristic

The xa-heuristic example shows you how to make an XA heuristic decision through JBoss Messaging Management Interface. A heuristic decision is a unilateral decision to commit or rollback an XA transaction branch after it has been prepared.

9.1.52. XA Receive

The xa-receive example shows you how message receiving behaves in an XA transaction in JBoss Messaging.

9.1.53. XA Send

The xa-send example shows you how message sending behaves in an XA transaction in JBoss Messaging.

9.1.54. XA with Transaction Manager

The xa-with-jta example shows you how to use JTA interfaces to control transactions with JBoss Messaging.

9.2. Core API Examples

To run a core example, simply cd into the appropriate example directory and type ant

9.2.1. Embedded

This example shows how to embed the JBoss Messaging server within your own code.

9.3. Java EE Examples

Most of the Java EE examples can be run the following way. simply cd into the appropriate example directory an type ant deploy. This will create a new JBoss AS profile and start the server. When the server is started from a different window type ant run to run the example. Some examples require further steps, please refer to the examples documentation for further instructions.

9.3.1. EJB/JMS Transaction

An example that shows using an EJB and JMS together within a transaction.

9.3.2. HAJNDI (High Availability)

A simple example demonstrating using JNDI within a cluster.

9.3.3. Resource Adapter Configuration

This example demonstrates how to configure several properties on the JBoss Messaging JCA resource adaptor.

9.3.4. JMS Bridge

An example demonstrating the use of the JBoss Messaging JMS bridge.

9.3.5. MDB (Message Driven Bean)

A simple example of a message driven bean.

9.3.6. Servlet Transport

An example of how to use the JBoss Messaging servlet transport.

9.3.7. XA Recovery

An example of how XA recovery works within the JBoss Application server using JBoss Messaging.