-
-
Save johnament/1225081 to your computer and use it in GitHub Desktop.
Resource Producer proposal for JMS 2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 @FooMyTopic | |
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 @Foo Session session; | |
@Inject @FooCallBack MessageConsumer callBackConsumer; | |
@Inject @FooCallBack TemporaryQueue temporaryReplyQueue; | |
@Inject @FooMyTopic MessageProducer topicPublisher; | |
@Inject @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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JDA: Durable represents durable objects, used in topics. | |
*/ | |
@Target({ TYPE, METHOD, PARAMETER, FIELD }) | |
@Retention(RUNTIME) | |
@Documented | |
@Qualifier | |
public @interface Durable { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JDA: Represents call back objects as a qualifier. | |
*/ | |
@Target({ TYPE, METHOD, PARAMETER, FIELD }) | |
@Retention(RUNTIME) | |
@Documented | |
@Qualifier | |
public @interface FooCallBack { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* JDA: FooMyTopic represents some Topic in the JNDI space. | |
*/ | |
@Target({ TYPE, METHOD, PARAMETER, FIELD }) | |
@Retention(RUNTIME) | |
@Documented | |
@Qualifier | |
public @interface FooMyTopic { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Represents all injectable resources for this use case. | |
* | |
* @author jament | |
*/ | |
@RequestScoped //or application, or session... | |
public class ResourceV4 { | |
@Inject Session session; | |
private TemporaryQueue callBackQueue; | |
private MessageProducer fooMyTopic; | |
private MessageConsumer callBackConsumer; | |
private MessageConsumer durableConsumer; | |
@Produces @FooMyTopic | |
@Resource(mappedName="jms/MyTopic") | |
Topic destination; | |
@PostConstruct | |
public void init () throws JMSException { | |
callBackQueue = session.createTemporaryQueue(); | |
fooMyTopic = session.createProducer(destination); | |
callBackConsumer = session.createConsumer(callBackQueue); | |
durableConsumer = session.createDurableSubscriber(destination, "resourceV4"); | |
} | |
@Produces @FooCallBack | |
public TemporaryQueue produceTemporaryQueue() { | |
return callBackQueue; | |
} | |
@Produces @Foo | |
public Session produceSession() { | |
return session; | |
} | |
@Produces @FooMyTopic | |
public MessageProducer produceMessageProducer() throws JMSException { | |
return fooMyTopic; | |
} | |
@Produces @FooCallBack | |
public MessageConsumer produceMessageConsumer() throws JMSException { | |
return callBackConsumer; | |
} | |
@Produces @Durable | |
public MessageConsumer produceDurableMessageConsumer() throws JMSException { | |
return durableConsumer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment