Similar to step 4, omit the steps for context lookup, use dependency injection only.
8.1 Access the JMS resources in EJB
In ibm-ejb-jar-bnd.xml under META-INF
<session name="jmsService"> <resource-ref name="jms/MyTopicConnectionFactory" binding-name="jms/TopicConnectionFactory"> </resource-ref> <message-destination-ref name="jms/MyTopic" binding-name="jms/Topic"/> </session>
Note: The binding-name attribute of <resource-ref> and <message-destination-ref> element should match the jndi name specified in Step 6 and Step 7.
Create a stateless session bean
@Stateless(name = "jmsService") @Local(JMSService.class) public class JMSServiceBean implements JMSService { @Resource(name = "jms/MyTopicConnectionFactory") private TopicConnectionFactory tcf; @Resource(name = "jms/MyTopic") private Topic topic; @Override public void broadcast() { try { TopicConnection connection = tcf.createTopicConnection(); TopicSession session = connection.createTopicSession (false, TopicSession.AUTO_ACKNOWLEDGE); TopicPublisher publisher = session.createPublisher(topic); TextMessage message = session.createTextMessage(); message.setText("This is a broadcast"); publisher.send(message); } catch (JMSException e) { throw new EJBException(e); } System.out.println("Broadcasted"); } }
Note: the name attribute of @Resource annotation should match name attribute of <resource-ref> and <message-destination-ref> element respectively in ibm-ejb-jar-bnd.xml, not the binding-name attribute.
Write a servlet to test
public class JMSTopicPublishServlet extends HttpServlet { @EJB(beanName="jmsService") private JMSService jmsService; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { jmsService.broadcast(); } }
Result
[26/08/11 15:18:00:625 EST] 0000008f SystemOut O Broadcasted
8.2 Access the JMS resources in servlet
In ibm-web-bnd.xml under WEB-INF
<resource-ref name="jms/MyTopicConnectionFactory" binding-name="jms/TopicConnectionFactory"> </resource-ref> <message-destination-ref name="jms/MyTopic" binding-name="jms/Topic"/>
Note: The binding-name attribute of <resource-ref> and <message-destination-ref> element should match the jndi name specified in Step 6 and Step 7.
Write a servlet to test
public class JMSTopicPublishServlet extends HttpServlet { @Resource(name = "jms/MyTopicConnectionFactory") private TopicConnectionFactory tcf; @Resource(name = "jms/MyTopic") private Topic topic; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { TopicConnection connection = tcf.createTopicConnection(); TopicSession session = connection.createTopicSession (false, TopicSession.AUTO_ACKNOWLEDGE); TopicPublisher publisher = session.createPublisher(topic); TextMessage message = session.createTextMessage(); message.setText("This is a broadcast"); publisher.send(message); } catch (JMSException e) { throw new EJBException(e); } System.out.println("Broadcasted"); } }
Result
[26/08/11 15:23:36:218 EST] 0000008f SystemOut O Broadcasted
Step 9: Create Topic Message Driven Beans
9.1 Create a Topic Activation Specification
Similar to 5.1
Type TopicActSpec for Name
jms/TopicActSpec for JNDI name
Select Topic as Destination type
Type jms/Topic for Destination JNDI name
Select InternalJMS as Bus name
Click ‘OK’ and save the changes.
9.2 Create two Topic Message Driven Beans
@MessageDriven(name="topicMessageDrivenBean1") public class TopicMessageDrivenBean1 implements MessageListener { @Override public void onMessage(Message message) { try { TextMessage txtMessage = (TextMessage) message; System.out.println(txtMessage.getText() + " processed by bean 1"); } catch (JMSException ex) { throw new EJBException(ex); } } } @MessageDriven(name="topicMessageDrivenBean2") public class TopicMessageDrivenBean2 implements MessageListener { @Override public void onMessage(Message message) { try { TextMessage txtMessage = (TextMessage) message; System.out.println(txtMessage.getText() + " processed by bean 2"); } catch (JMSException ex) { throw new EJBException(ex); } } }
In ibm-ejb-jar-bnd.xml under META-INF
<message-driven name="topicMessageDrivenBean1"> <jca-adapter activation-spec-binding-name="jms/TopicActSpec"/> </message-driven> <message-driven name="topicMessageDrivenBean2"> <jca-adapter activation-spec-binding-name="jms/TopicActSpec"/> </message-driven>
Test the servlet created in step 8 and check the result
[26/08/11 15:23:36:218 EST] 0000008f SystemOut O Broadcasted
[26/08/11 15:23:36:218 EST] 000000a3 SystemOut O This is a broadcast processed by bean 2
[26/08/11 15:23:36:218 EST] 000000a6 SystemOut O This is a broadcast processed by bean 1
No comments:
Post a Comment