Skip to content

Martini Invoking Services via a JMS Trigger

Overview

The JMS listener trigger enables developers to build applications that respond to messages via the Java Message Service (JMS), a standard for implementing reliable, asynchronous, and loosely-coupled communications. Apache ActiveMQ is the default message broker in Martini, but it can be configured to use ActiveMQ Artemis or RabbitMQ as alternatives.

Ad Hoc Message Publishing

Martini allows for ad hoc JMS message sending from the UI. By right-clicking an instance in the Navigator view in either Martini Desktop or Online and selecting "Send JMS Message," you can specify a destination and message content.

Properties

JMS Listener-Specific Configuration

The configuration properties specific to the JMS listener trigger include:

Property Default Description
Destination (required) The JMS destination this trigger should listen to.
Transaction Safe false Indicates if the service invocation should be wrapped in a transaction.
Acknowledgement Auto acknowledge The acknowledgement mode for non-transacted sessions, determining how messages are acknowledged.
Durable false Whether a durable subscription is used, ensuring messages are received across restarts.
Concurrent Consumers 1 The number of concurrent consumers created for the specified destination.
Max Concurrent Consumers 1 The maximum number of concurrent consumers that can be created for the destination, dynamically increasing based on incoming message volume.

Warnings

  • Concurrent Consumption of the Same Message: With more than one concurrent consumer for a topic, there's a risk of the same message being consumed concurrently, especially if the broker is not JMS 2.0 compliant.
  • Loss of Message Ordering: Increasing the number of concurrent consumers can lead to the loss of guaranteed message ordering. This is more critical for low-volume queues where order is important.

Service Parameters

JMS listener-specific parameters include:

Name Type Description
message javax.jms.Message The JMS message that triggered the service.
destination javax.jms.Destination The JMS destination of the message.
destinationName java.lang.String The name of the JMS destination as a string.
replyTo javax.jms.Destination The JMS reply-to destination, or null if not specified.
correlationId java.lang.String The correlation ID of the message, or null if not available.

Depending on the type of message, additional variables are exposed:

  • BytesMessage: Access to byte array content of the message.
  • MapMessage: Interaction with the map content of the message.
  • ObjectMessage: Direct access to the serialized object within the message.
  • StreamMessage: Interaction with the stream content of the message.
  • TextMessage: Access to the text content of the message.

Best Practices and Troubleshooting

For optimal use of JMS listener triggers in Martini, consider the following:

  • Ensure your JMS broker setup is compliant with JMS 2.0 if using multiple concurrent consumers for topics to avoid duplicate message consumption.
  • Maintain message ordering by carefully considering the impact of increasing concurrent consumers, especially for queues with low volume and high importance on order.

Usage Examples

Groovy Script as a Service

A Groovy script can serve as a service for logging context variables when a JMS message is received:

1
2
3
println "Destination: ${destination}"
println "Message: ${message}"
// Further processing logic here

This script logs the destination and the message content, demonstrating a simple way to process JMS messages in Martini.

Handling Messages with Groovy Methods

Groovy methods provide a powerful way to process incoming JMS messages. Here’s an example method for handling such messages:

1
2
3
4
5
6
class MessageResponder {
    void respondToMessage(javax.jms.Message message, javax.jms.Destination destination) {
        println "Received message from ${destination}"
        // Additional logic to process the message and optionally respond
    }
}

This class defines a method that logs the receipt of a message and outlines a structure for further message processing and response.

Best Practices and Troubleshooting

Consider the following when working with JMS listener triggers:

  • Manage concurrent consumers on topics carefully to prevent the same message from being processed multiple times.
  • Note that increasing the number of concurrent consumers can disrupt the order of message processing.
  • Check Martini logs regularly for troubleshooting and to ensure smooth operation.

For more detailed examples and to explore advanced configurations, refer to the examples Martini package included with the distribution.