-
-
Save pmuir/1226432 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 @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; | |
} | |
} |
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. | |
* PLM: Is this application defined? | |
*/ | |
@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
/** 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 { | |
@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