Im new to java message-driven beans and i have a little problem. I made a test web project(server is glassfish 4.0).
In messageclient, i'm sending 3 Strings to message queue. If i understand message-driven beans correctly, message-driven bean should write all values out in my console, but instead it writes only the first value in queue. I'm trying to implement this because i need different thread to send emails to customer from my web application. Is there maybe a better way?
here is the code of my message client:
@Named(value = "messageDrivenbean")
@ViewScoped
public class MessageDrivenBeanClient implements Serializable
{
ArrayList <String>mail;
@Resource(name="connFactory", mappedName="mailConnFactory")
private QueueConnectionFactory QueueConnectionFactory;
@Resource(name="jmsQueue", mappedName="Queue")
private Queue queue;
/**
* Creates a new instance of testingbean
*/
public MessageDrivenBeanClient()
{
}
@PostConstruct
public void init(){
}
Connection conn;
public void start() throws JMSException
{
String text;
Message msg = null;
final int NUM_MSGS = 3;
conn = QueueConnectionFactory.createConnection();
Session sesion=conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer msgProducer=sesion.createProducer(queue);
msg=sesion.createTextMessage();
for (int i = 0; i < NUM_MSGS; i++) {
text = "This is message " + (i + 1);
System.out.println("Sending message: " + text);
msg.setStringProperty("message", text);
msgProducer.send(msg);
}
}
}
And my message-driven bean:
@MessageDriven(activationConfig =
{
@ActivationConfigProperty
(propertyName="destinationType",propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "Queue")
})
public class MsgDrivnBean implements MessageListener
{
@Resource
private MessageDrivenContext mdc;
public MsgDrivnBean()
{
}
@Override
public void onMessage(Message message)
{
TextMessage tm=null;
try{
if(message instanceof TextMessage){
tm= (TextMessage) message;
System.out.println(message.getStringProperty("message"));
System.out.print("sem v msgdrivenbeanu"+ tm.getStringProperty("message"));
//logger.error(tm.getText());
}
else
{
System.out.println("Error");
}
}
catch(JMSException jms)
{
jms.printStackTrace();
mdc.setRollbackOnly();
}
}
}
My current output is:
Sending message: This is message 1
INFO: Sending message: This is message 2
INFO: Sending message: This is message 3
INFO: This is message 1
INFO: sem v msgdrivenbeanuThis is message 1
I think it should also write out message 2 and message 3, but it doesn't.
Please help me.