1 / 13

MessageDriven-Beans

MessageDriven-Beans. Eric F. Gerlofsma eric.gerlofsma@hu.nl. JMS-Based Message-Driven Beans. Message-driven beans (MDBs) are: stateless server-side transaction-aware components for processing asynchronous messages delivered via JMS. Its container manages:

lupita
Download Presentation

MessageDriven-Beans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MessageDriven-Beans Eric F. Gerlofsma eric.gerlofsma@hu.nl

  2. JMS-Based Message-Driven Beans • Message-driven beans (MDBs) are: • stateless • server-side • transaction-aware • components for processing asynchronous messages delivered via JMS. • Its container manages: • the component’s environment • transactions • security • resources • concurrency • message acknowledgment • Message-driven beans doesn’t have a remote or local business interface. • Message-driven beans do have a Listener interface.

  3. JMS-Based Message-Driven Beans • Message-driven beans (MDBs) are: • Annotated POJO’s, • @MessageDriven • @ActivationConfigProperty • destinationType • destination • implementing the javax.jms.MessageListener interface • public void onMessage(Message message) • and called by MBean (Management Bean)

  4. MDB, SLSB SLSB instances JNDI MessageListener void onMessage(Message message) JNDI Queue MDB instances

  5. Example Message-Driven Bean package efg.mdb; import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.Message; import javax.jms.MessageListener; @MessageDriven ( activationConfig = { @ActivationConfigProperty( propertyName = "destinationType", propertyValue = "javax.jms.Queue" ) , @ActivationConfigProperty( propertyName = "destination“ , propertyValue = "queue/A" ) } ) public class ExampleMDB implements MessageListener { public void onMessage(Message message) { System.out.println(message); } }

  6. Create a Queue In directory: C:\jboss-5.1.0.GA\server\default\deploy\messaging remove the file: destinations-service.xml insert from directory: C:\jboss-5.1.0.GA\docs\examples\jms the file: example-destinations-service.xml The result is: Global JNDI Namespace +- queue (class: org.jnp.interfaces.NamingContext) | +- C (class: org.jboss.jms.destination.JBossQueue) | +- D (class: org.jboss.jms.destination.JBossQueue) | +- QueueWithOwnDLQAndExpiryQueue (class: org.jboss.jms.destination.JBossQueue) | +- ABC-TransactionManager (class: org.jboss.jms.destination.JBossQueue) | +- QueueWithOwnRedeliveryDelay (class: org.jboss.jms.destination.JBossQueue) | +- PrivateExpiryQueue (class: org.jboss.jms.destination.JBossQueue) | +- PrivateDLQ (class: org.jboss.jms.destination.JBossQueue) | +- DLQ (class: org.jboss.jms.destination.JBossQueue) | +- A (class: org.jboss.jms.destination.JBossQueue) | +- testQueue (class: org.jboss.jms.destination.JBossQueue) | +- ExpiryQueue (class: org.jboss.jms.destination.JBossQueue) | +- testDistributedQueue (class: org.jboss.jms.destination.JBossQueue) | +- B (class: org.jboss.jms.destination.JBossQueue) | +- ex (class: org.jboss.jms.destination.JBossQueue)

  7. Example Calling Message-Driven Bean import . . . public class Main { public static void main(String[] args) throws Exception { String mq = "queue/A"; String cf = "ConnectionFactory"; QueueConnection qc = null; try { Context context = new InitialContext(); Queue q = (Queue)context.lookup(mq); QueueConnectionFactory qcf = (QueueConnectionFactory)context.lookup(cf); qc = qcf.createQueueConnection(); // no credentials! QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); QueueSender qsender = qs.createSender(q); TextMessage tm = qs.createTextMessage(); tm.setText("Hello World!"); qsender.send(tm); } finally { if (qc != null) { qc.close(); } } } } create tm factory connection session lookup create create qcf qc qs create qsender send queue lookup q

  8. MessageDriven package javax.ejb; @Target(TYPE) @Retention(RUNTIME) public @interface MessageDriven { String description default ””; Class messageListenerInterface default Object.class; String name default class name; String mappedName default ””; ActivationConfigProperty[] activationConfig default {}; } mappedName – A product specific name ( e.g. global JNDI name of a queue ) that this message-driven bean should be mapped to. name – internal ejb-name for this bean mesageListenerInterface – not used in JBoss

  9. ActivationConfigProperties Type String String String int String String String Remarks The jndi name of the Queue or Topic javax.jms.Queue | javax.jms.Topic AUTO_AKNOWLEDGE | DUPS_OK_ACKNOWLEDGE Durable | NonDurable Name destination destinationType messageSelector acknowledgeMode clientID subscriptionDurability subscriptionName and 19 more JBoss extensions!

  10. MDB, SLSB, SecurityDomain SLSB instances Role-based Access JNDI MessageListener void onMessage(Message message) Role-based Access Role-based Listen Access JNDI Queue MDB instances

  11. Queue JMSSecurityDomain =JmsXARealm META-INF/secureQueue-service.xml <?xml version="1.0" encoding="UTF-8"?> <server> <mbean code="org.jboss.jms.server.destination.QueueService" name="jboss.messaging.destination:service=Queue,name=secureQueue" xmbean-dd="xmdesc/Queue-xmbean.xml"> <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends> <depends>jboss.messaging:service=PostOffice</depends> <attribute name="SecurityConfig"> <security> <role name="j2ee" read="true"/> <role name="guest" write="true"/> </security> </attribute> </mbean> </server> If the read attribute is true then that role will be able to create consumers and receive messages. If the write attribute is true then that role will be able to create producers and send messages. If the create attribute is true then that role will be able to create durable subscriptions on topics. See the Hypersonic database PUBLIC.JBM_... tables: in particular: PUBLIC.JBM_ROLE and PUBLIC.JBM_USER for default usernames passwords and roles.

  12. Secure Message-Driven Bean package efg.mdb; import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.Message; import javax.jms.MessageListener; @MessageDriven ( activationConfig = { @ActivationConfigProperty( propertyName = "destinationType", propertyValue = "javax.jms.Queue" ) , @ActivationConfigProperty( propertyName = "destination" , propertyValue = "queue/secureQueue") , @ActivationConfigProperty( propertyName = "user" , propertyValue = "guest") , @ActivationConfigProperty( propertyName = "password" , propertyValue = "guest") } ) public class ExampleMDB implements MessageListener { public void onMessage(Message message) { System.out.println(message); } }

  13. Secure Calling Message-Driven Bean import . . . public class Main { public static void main(String[] args) throws Exception { String mq = "queue/secureQueue"; String cf = "ConnectionFactory"; QueueConnection qc = null; try { Context context = new InitialContext(); Queue q = (Queue)context.lookup(mq); QueueConnectionFactory qcf = (QueueConnectionFactory)context.lookup(cf); qc = qcf.createQueueConnection("quest", "quest"); QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); QueueSender qsender = qs.createSender(q); TextMessage tm = qs.createTextMessage(); tm.setText("Hello World!"); qsender.send(tm); } finally { if (qc != null) { qc.close(); } } } } create tm factory connection session lookup create create qcf qc qs create qsender send queue lookup q

More Related