Diferencia entre revisiones de «JMS Con GlassFish»
(→Enviar un mensaje) |
|||
| Línea 17: | Línea 17: | ||
<code java> | <code java> | ||
Properties props = new Properties(); | Properties props = new Properties(); | ||
| − | props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory"); | + | props.setProperty("java.naming.factory.initial", |
| − | props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming"); | + | "com.sun.enterprise.naming.SerialInitContextFactory"); |
| − | props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl"); | + | props.setProperty("java.naming.factory.url.pkgs", |
| − | props.setProperty("java.naming.provider.url", "localhost"); | + | "com.sun.enterprise.naming"); |
| + | props.setProperty("java.naming.factory.state", | ||
| + | "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl"); | ||
| + | props.setProperty("java.naming.provider.url", | ||
| + | "localhost"); | ||
| + | |||
Context jndiContext = new InitialContext(props); | Context jndiContext = new InitialContext(props); | ||
| − | QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("jms/ | + | QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("jms/holaMundoConnectionFactory"); |
Queue queue = (Queue) jndiContext.lookup("jms/holaMundoQueue"); | Queue queue = (Queue) jndiContext.lookup("jms/holaMundoQueue"); | ||
QueueConnection queueCon = null; | QueueConnection queueCon = null; | ||
| Línea 40: | Línea 45: | ||
</code> | </code> | ||
| − | |||
==Ver también== | ==Ver también== | ||
[https://glassfish.dev.java.net/nonav/javaee5/docs/DG/beakt.html#beali Documentación de GlassFish] | [https://glassfish.dev.java.net/nonav/javaee5/docs/DG/beakt.html#beali Documentación de GlassFish] | ||
Revisión del 16:06 31 mar 2009
Para utilizar JMS con GlassFish es necesario incluir en el classpath del cliente los siguientes Jars que pueden encontrarse en el directorio de instalación de GlassFish:
directorio-de-glassfish/lib/appserv-rt.jar
directorio-de-glassfish/lib/j2ee.jar
directorio-de-glassfish/lib/install/aplications/jmsra/imqjmsra.jar
Encontré también necesario incluir
directorio-de-glassfish/lib/appserv-admin.jar
Enviar un mensaje
Para enviar un mensaje desde un cliente remoto, es necesario incluir los JAR de GlassFiss antes mencionados, y configurar el Context con las variables correspondientes.
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state",
"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
props.setProperty("java.naming.provider.url",
"localhost");
Context jndiContext = new InitialContext(props);
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("jms/holaMundoConnectionFactory"); Queue queue = (Queue) jndiContext.lookup("jms/holaMundoQueue"); QueueConnection queueCon = null;
try {
queueCon = queueConnectionFactory.createQueueConnection();
QueueSession session = queueCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender sender = session.createSender(queue);
TextMessage message = session.createTextMessage();
message.setText("Hola, mundo");
sender.send(message);
} finally {
if (queueCon != null) {
queueCon.close();
}