Skip to content

Instantly share code, notes, and snippets.

@pmuir
Forked from johnament/ApplicationConsumer.java
Created September 19, 2011 12:48
Show Gist options
  • Save pmuir/1226432 to your computer and use it in GitHub Desktop.
Save pmuir/1226432 to your computer and use it in GitHub Desktop.
Resource Producer proposal for JMS 2.0
/**
* PLM: ApplicationConsumer is a class provided by the application, and injects and
* uses the resources defined in the central Resources class. An application would
* typically have multiple ApplicationConsumer-style classes - this is just an
* example
*/
public class ApplicationConsumer {
// regular destination.
@Inject @Foo
Topic destination;
// session used to receive replies
// JDA: assuming an EE environment, not sure we would need
// to have a qualifier on Session in this case, since there
// is only one type of valid session in EE.
@Inject Session session;
@Inject @Foo MessageConsumer callBackConsumer;
@Inject @Foo TemporaryQueue temporaryReplyQueue;
@Inject @Foo MessageProducer topicPublisher;
@Inject @Foo @Durable MessageConsumer durableConsumer;
@PostConstruct
public void setupAutoResponder () throws JMSException {
FooListener fl = new FooListener(session);
durableConsumer.setMessageListener(fl);
}
/**
* Send a request to requestQueue and receive the reply using a temporary queue
* @param request
* @throws JMSException
*/
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public String sendRequestAndReceiveReply2(String request) throws JMSException {
// PLM: Note that I have not injected the request message, as it seems simpler to create it through the JMS as defined today.
// Potentially this is an area where the JMS API could be made more CDI friendly. In order to comment I would need to understand the use cases better.
TextMessage textMessage = session.createTextMessage(request);
textMessage.setJMSReplyTo(temporaryReplyQueue);
topicPublisher.send(textMessage);
TextMessage reply = (TextMessage) callBackConsumer.receive(3000);
String replyString=reply.getText();
return replyString;
}
}
/**
* JDA: Durable represents durable objects, used in topics.
* PLM: Is this application defined?
*/
@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface Durable {
}
/**
* PLM: Foo is a user defined qualifier. An application would have one qualifier per "group" JMS resources defined.
*/
@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface Foo {
}
/** Message listener that sends a response when it gets a message.
*
* @author jament
*/
public class FooListener implements javax.jms.MessageListener {
private Session session;
public FooListener(Session session) {
this.session = session;
}
@Override
public void onMessage(Message message) {
if(message instanceof TextMessage) {
try {
TextMessage tm = (TextMessage) message;
System.out.println("Received message was: "+tm.getText());
Destination replyTo = tm.getJMSReplyTo();
if (replyTo != null) {
MessageProducer mp = session.createProducer(replyTo);
TextMessage response = session.createTextMessage();
response.setText("I have received your message.");
mp.send(response);
mp.close();
}
} catch (JMSException ex) {
ex.printStackTrace();
}
}
}
}
/**
* JDA: FooMyTopic represents some Topic in the JNDI space.
*/
@Target({ TYPE, METHOD, PARAMETER, FIELD })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface FooMyTopic {
}
/**
*
* Represents all injectable resources for this use case.
*
* @author jament
*/
@RequestScoped //or application, or session...
public class ResourceV4 {
@Produces @Foo
private TemporaryQueue callBackQueue;
@Produces @Foo
private MessageProducer fooMessageProducer;
@Produces @Foo
private MessageConsumer callBackConsumer;
@Produces @Foo @Durable
private MessageConsumer durableConsumer;
@Produces @Foo
@Resource(mappedName="jms/FooTopic")
Topic destination;
@PostConstruct
public void init () throws JMSException {
callBackQueue = session.createTemporaryQueue();
fooMyTopic = session.createProducer(destination);
callBackConsumer = session.createConsumer(callBackQueue);
durableConsumer = session.createDurableSubscriber(destination, "resourceV4");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment