如何在web-app中访问远程的jms server

xiaomaju 2011-08-17 03:06:55
请教,我写了个web application,部署在tomcat6中,需要访问远程的jboss5 jms服务器,发送和收取消息,请问应该如何配置我的tomcat?谢谢
...全文
54 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
LMAOhuaNL 2011-08-17
  • 打赏
  • 举报
回复
在glassfish中设置远程jms server


import javax.jms.*;
import javax.naming.*;

/**
* simple hello world consumer
*/
public class HelloConsumer
{
/**
* simple consumer
*/
public HelloConsumer() {
try {
Hashtable env;
Context ctx = null;

env = new Hashtable();

// Store the environment variables that tell JNDI which initial context
// to use and where to find the provider.

// For use with the File System JNDI Service Provider
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:///tmp/jndi");

try {
// Create the initial context.
ctx = new InitialContext(env);
} catch (NamingException ex) {
ex.printStackTrace();
System.exit(-1);
}

// creating a connection factory

ConnectionFactory cf= new com.sun.messaging.ConnectionFactory();
ConnectionFactory cf = null;
try {
// Lookup my connection factory from the admin object store.
// The name used here here must match the lookup name
// used when the admin object was stored.
System.out.println("Looking up Connection Factory object with lookup name: HelloConnectionFactory");
cf = (javax.jms.ConnectionFactory) ctx.lookup("HelloConnectionFactory");
System.out.println("Connection Factory object found.");
} catch (NamingException ne) {
System.err.println("Failed to lookup Connection Factory object.");
ne.printStackTrace();
System.exit(-1);
}


// create a connection
Connection connection = cf.createConnection();

// create a session
Session session = connection.createSession(
false /* not transacted */, Session.AUTO_ACKNOWLEDGE);

// create destination HelloWorld
Destination destination = session.createQueue("HelloWorld");

Queue queue = null;
try {
// Lookup my queue from the admin object store.
// The name I search for here must match the lookup name used when
// the admin object was stored.
System.out.println("Looking up Queue object with lookup name: HelloQueue");
queue = (javax.jms.Queue)ctx.lookup("HelloQueue");
System.out.println("Queue object found.");
} catch (NamingException ne) {
System.err.println("Failed to lookup Queue object.");
ne.printStackTrace();
System.exit(-1);
}

// create a consumer
MessageConsumer consumer = session.createConsumer(destination);

// now that everything is ready to go, start the connection
connection.start();

// receive our message
TextMessage m = (TextMessage)consumer.receive();

System.out.println(m.getText());

// close everything
consumer.close();
session.close();
connection.close();


} catch (JMSException ex) {
System.out.println("Error running program");
ex.printStackTrace();
}
}


/**
* main method
*/
public static void main(String args[]) {
new HelloConsumer();
}
}
lequery 2011-08-17
  • 打赏
  • 举报
回复
简单的配置搞不定吧,要写程序啊

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧