Chapter 26. JMS User’s Guide
199
26.2.2. Accessing the Destination Object
Accessing a JMS destination within the code of an application component requires using a
Resource
Environment Reference
, which is represented in the standard deployment descriptor as follows:
Ê
resource-env-ref
Ë
Ê
resource-env-ref-name
Ë
jms/stockQueue
Ê
/resource-env-ref-name
Ë
Ê
resource-env-ref-type
Ë
javax.jms.Queue
Ê
resource-env-ref-type
Ë
Ê
/resource-env-ref
Ë
The application component’s source code should contain:
Queue q = (Queue) ctx.lookup("java:comp/env/jms/stockQueue");
The mapping to the actual JNDI name (for example, "myQueue") is defined in the JOnAS-specific
deployment descriptor in the following way:
Ê
jonas-resource-env
Ë
Ê
resource-env-ref-name
Ë
jms/stockQueue
Ê
/resource-env-ref-name
Ë
Ê
jndi-name
Ë
myQueue
Ê
jndi-name
Ë
Ê
/jonas-resource-env
Ë
26.2.3. Writing JMS Operations
A typical method performing a message-sending JMS operation looks like the following:
void sendMyMessage() {
ConnectionFactory cf = (ConnectionFactory)
ctx.lookup("java:comp/env/jms/conFact");
Queue queue = (Queue) ctx.lookup("java:comp/env/jms/stockQueue");
Connection conn = cf.createConnection();
Session sess = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
MessageProducer
mp = sess.createProducer((Destination)queue);
ObjectMessage msg = sess.createObjectMessage();
msg.setObject("Hello");
sender.send(msg);
sess.close();
conn.close();
}
It is also possible for an application component to
synchronously
receive a message. Here is an EJB
method that performs synchronous message reception on a queue:
public String recMsg() {
ConnectionFactory cf = (ConnectionFactory)
ctx.lookup("java:comp/env/jms/conFact");
Queue queue = (Queue) ctx.lookup("java:comp/env/jms/stockQueue");
Connection conn = cf.createConnection();
Session sess = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
MessageConsumer mc = sess.createConsumer((Destination)queue);
conn.start();
ObjectMessage msg = (ObjectMessage) mc.receive();
String msgtxt =
(String) msg.getObject();
sess.close();
conn.close();
return msgtxt;
}
Содержание Application Server
Страница 1: ...Red Hat Application Server JOnAS User Guide ...
Страница 8: ......
Страница 22: ...14 Chapter 1 Java Open Application Server JOnAS a J2EE Platform ...
Страница 58: ...50 Chapter 3 JOnAS Configuration ...
Страница 66: ...58 Chapter 5 JOnAS Class Loader Hierarchy ...
Страница 78: ...70 Chapter 6 JOnAS Command Reference ...
Страница 80: ......
Страница 86: ...78 Chapter 7 Developing Session Beans ...
Страница 136: ...128 Chapter 9 Developing Message Driven Beans ...
Страница 142: ...134 Chapter 10 Defining the Deployment Descriptor ...
Страница 148: ...140 Chapter 11 Transactional Behavior of EJB Applications ...
Страница 158: ...150 Chapter 14 EJB Packaging ...
Страница 162: ...154 Chapter 15 Application Deployment and Installation Guide ...
Страница 164: ......
Страница 176: ...168 Chapter 18 WAR Packaging ...
Страница 178: ......
Страница 184: ...176 Chapter 20 Defining the Client Deployment Descriptor ...
Страница 186: ...178 Chapter 21 Client Packaging ...
Страница 188: ......
Страница 192: ...184 Chapter 23 EAR Packaging ...
Страница 194: ......
Страница 200: ...192 Chapter 24 JOnAS Services ...
Страница 204: ...196 Chapter 25 JOnAS and the Connector Architecture ...
Страница 222: ...214 Chapter 27 Ant EJB Tasks Using EJB JAR ...
Страница 234: ...226 Chapter 29 Web Services with JOnAS ...
Страница 236: ......
Страница 260: ...252 Chapter 34 How to use Axis in JOnAS ...
Страница 270: ...262 Chapter 36 Web Service Interoperability between JOnAS and BEA WebLogic ...
Страница 296: ......