import javax.ejb.MessageDriven;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.Inject;
import javax.jms.*;
import java.util.*;
import javax.ejb.TimedObject;
import javax.ejb.Timer;
import javax.ejb.TimerService;
@MessageDriven(
activationConfig = {
@ActivationConfigProperty(propertyName="connectionFactoryJndiName",
propertyValue="jms/TopicConnectionFactory"),
@ActivationConfigProperty(propertyName=
"destinationName", propertyValue="jms/myTopic"),
@ActivationConfigProperty(propertyName=
"destinationType", propertyValue="javax.jms.Topic"),
@ActivationConfigProperty(propertyName=
"messageSelector", propertyValue="RECIPIENT = 'MDB'") } )
/** A simple Message-Driven Bean that listens to the configured JMS Queue or
Topic and gets notified via an * invocation of it's onMessage() method
when a message has been posted to the Queue or Topic.The bean
* prints the contents of the message. */
public class MessageLogger implements MessageListener, TimedObject
{
@Inject javax.ejb.MessageDrivenContext mc;
public void onMessage(Message message)
{ System.out.println("onMessage() - " + message);
try
{
String subject = message.getStringProperty("subject");
String inmessage = message.getStringProperty("message");
System.out.println("Message received\n\tDate:" + new java.util.Date()
+ "\n\tSubject:" + subject + "\n\tMessage:" + inmessage + "\n");
System.out.println("Creating Timer a single event timer");
TimerService ts = mc.getTimerService();
Timer timer = ts.createTimer(30000, subject);
System.out.println("Timer created by MDB at:"
+ new Date(System.currentTimeMillis()) +" with info:"+subject); }
catch (Throwable ex)
{ ex.printStackTrace(); }
}
public void ejbTimeout(Timer timer)
{ System.out.println("EJB 3.0:Timer with MDB");
System.out.println("ejbTimeout() called at:"
+ new Date(System.currentTimeMillis()));
return; }
}