package org.glassfish.test.jms.injection.ejb;
import javax.annotation.Resource;
import javax.ejb.*;
import javax.inject.Inject;
import javax.jms.*;
/**
*
* @author LILIZHAO
*/
@Stateless(mappedName="SessionBeanInjection/remote")
public class SessionBeanInjection implements SessionBeanInjectionRemote {
private static String transactionScope = "around TransactionScoped";
static String context;
@Resource(mappedName = "jms/jms_unit_test_Queue")
private Queue queue;
@Inject
@JMSConnectionFactory("jms/jms_unit_test_QCF")
@JMSSessionMode(JMSContext.AUTO_ACKNOWLEDGE)
private JMSContext jmsContext;
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Override
public void sendMessage(String text) {
try {
JMSProducer producer = jmsContext.createProducer();
TextMessage msg = jmsContext.createTextMessage(text);
producer.send(queue, msg);
context = jmsContext.toString();
} catch (Exception e) {
throw new EJBException(e);
}
}
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
@Override
public boolean checkMessageAndScoped(String text) {
System.out.println("JMSContext:"+ this.context);
if (this.context.indexOf(transactionScope) != -1){
System.out.println("The context variables used in the call are in transaction scope.");
} else {
System.out.println("TThe context variables used in the call are NOT in transaction scope.");
return false;
}
try {
JMSConsumer consumer = jmsContext.createConsumer(queue);
Message msg = consumer.receive(30000L);
if (msg instanceof TextMessage) {
String content = ((TextMessage) msg).getText();
if (text.equals(content))
return true;
}
return false;
} catch (Exception e) {
throw new EJBException(e);
}
}
}