@MessageDriven Annotation

@MessageDriven

The @MessageDriven annotation is used to mark the class as Message-Driven Bean. This annotation’s specification is as follows.

@Target(value=TYPE) @Retention(value=RUNTIME) public @interface MessageDriven { String description() default “”; String name() default “”; String mappedName() default “”; Class messageListenerInterface() default java.lang.Object.class ActivationConfigProperty[] activationConfig() default {}; }

Example:

@MessageDriven(
		name = "QueueMDB",
		description = "My First Queue MDB",
		mappedName = "queue/mdb",
		messageListenerInterface= MessageListener.class,
		activationConfig = {
			@ActivationConfigProperty(
                            propertyName = "destinationType", 
                            propertyValue = "javax.jms.Queue"),
			@ActivationConfigProperty(
                            propertyName = "destination", 
                            propertyValue = "queue/MyQueue") 
             }
)
public class QueueListenerMDB implements MessageListener {
    public void onMessage(Message message) {
        // Process the message
    }
}

Where,

  • name – MDB name. If omitted, then defaults to unqualified MDB class name (in our case, QueueListenerMDB)
  • description – String description of MDB
  • mappedName – A product specific name(e.g. global JNDI name of a queue) that this message-driven bean should be mapped to.
  • messageListenerInterface – MDB must directly (using implements keyword)or indirectly(using annotation) implement a message listener interface. If the MDB class implements more than one interface other than java.io.Serializable, java.io.Externalizable, or any of the interfaces defined by the javax.ejb package, the message listener interface must be specified.

@ActivationConfigProperty

  • activationConfig – it is an array of activation config properties for this MDB. The @ActivationConfigProperty annotation specification is as follows.
@Target(value={}) @Retention(value=RUNTIME) public @interface ActivationConfigProperty { String propertyName(); String propertyValue(); }

Some of the standard MDB activation configuration properties are,

Property Description
String destination The JNDI name of the Queue or Topic – MANDATORY
String destinationType The type of destination – valid values are javax.jms.Queue or javax.jms.Topic
String messageSelector The message selector of the subscription
int acknowledgeMode The type of acknowledgement when using non-transacted JMS – valid values are, AUTO_ACKNOWLEDGE ,
DUPS_OK_ACKNOWLEDGE.
Default is AUTO_ACKNOWLEDGE
String clientID the client id of the connection
boolean subscriptionDurability Whether topic subscriptions are durable – valid values Durable or NonDurable.
Default is NonDurable
String subscriptionName the subscription name of the topic subscription
subscriptionDurability
  • When using MDB topic, we can specify whether the topic subscription is durable or not.
  • In a publish-subscribe messaging model, a message is distributed to all subscribed consumers
  • Non durable subscription
    • A subscriber will not receive a copy of a message when he is not connected to a topic when a message arrived at the destination.
    • This is the default property value.

Example:

@MessageDriven(
	activationConfig = { 
	@ActivationConfigProperty(propertyName = "destinationType", 
			propertyValue = "javax.jms.Topic"),
	@ActivationConfigProperty(propertyName = "destination", 
			propertyValue = "topic/MyTopic"),
	@ActivationConfigProperty(propertyName = "subscriptionDurability", 
			propertyValue = "NonDurable")
	}
)
public class TopicListenerMDB1 implements MessageListener { }
  • Durable subscription
    • If a durable subscriber is not connected to a topic when a message is received, MOM retains a copy of the message until the subscriber connects and delivers the message.

Example:


@MessageDriven(
	activationConfig = { 
	@ActivationConfigProperty(propertyName = "destinationType", 
			propertyValue = "javax.jms.Topic"),
	@ActivationConfigProperty(propertyName = "destination", 
			propertyValue = "topic/MyTopic"),
	@ActivationConfigProperty(propertyName = "subscriptionDurability", 
			propertyValue = "Durable")
	}
)
public class TopicListenerMDB1 implements MessageListener { }
messageSelector
  • A message selector allows a JMS consumer to be more selective about the messages that it receives from a particular topic or queue.
  • A message selector uses message properties and headers as criteria in conditional expressions. These expressions use Boolean logic to declare which messages should be delivered to a client.
  • For example, if you want to get only those messages whose order amount is > 10000, you can use messageSelector as follows.

@MessageDriven(
	activationConfig = {
		@ActivationConfigProperty(propertyName = "destinationType",  
				propertyValue = "javax.jms.Queue"),					
		@ActivationConfigProperty(propertyName = "destination",  
				propertyValue = "queue/MyQueue") ,
		@ActivationConfigProperty(
 		propertyName = "messageSelector",  
				propertyValue = "OrderAmount > 10000") 
             }
)
public class QueueListenerMDB implements MessageListener {
    public void onMessage(Message message) {
        // Process the message
    }
}

In the sender, set the property as follows,

message.setIntProperty(“OrderAmount”, 12000);

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.