JMS入门的中的JNDI问题

wnba1983 2009-12-28 02:30:40
我在网上找了个代码,导入到Eclipse后没有语法错误,代码如下

SimpleQueueSender.java
/*
*
* Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the proprietary information of Sun
* Microsystems, Inc. Use is subject to license terms.
*
*/
/**
* The SimpleQueueSender class consists only of a main method,
* which sends several messages to a queue.
*
* Run this program in conjunction with SimpleQueueReceiver.
* Specify a queue name on the command line when you run the
* program. By default, the program sends one message. Specify
* a number after the queue name to send that number of messages.
*/
import javax.jms.*;
import javax.naming.*;

public class SimpleQueueSender {

/**
* Main method.
*
* @param args the queue used by the example and,
* optionally, the number of messages to send
*/
public static void main(String[] args) {
String queueName = null;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueSender queueSender = null;
TextMessage message = null;
final int NUM_MSGS;

if ( (args.length < 1) || (args.length > 2) ) {
System.out.println(”Usage: java SimpleQueueSender ” +
“ []”);
System.exit(1);
}
queueName = new String(args[0]);
System.out.println(”Queue name is ” + queueName);
if (args.length == 2){
NUM_MSGS = (new Integer(args[1])).intValue();
} else {
NUM_MSGS = 1;
}

/*
* Create a JNDI InitialContext object if none exists
* yet.
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println(”Could not create JNDI ” +
“context: ” + e.toString());
System.exit(1);
}

/*
* Look up connection factory and queue. If either does
* not exist, exit.
*/
try {
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup(”QueueConnectionFactory”);
queue = (Queue) jndiContext.lookup(queueName);
} catch (NamingException e) {
System.out.println(”JNDI lookup failed: ” +
e.toString());
System.exit(1);
}

/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create sender and text message.
* Send messages, varying text slightly.
* Send end-of-messages message.
* Finally, close connection.
*/
try {
queueConnection =
queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(queue);
message = queueSession.createTextMessage();
for (int i = 0; i < NUM_MSGS; i++) {
message.setText("This is message " + (i + 1));
System.out.println("Sending message: " +
message.getText());
queueSender.send(message);
}

/*
* Send a non-text control message indicating end of
* messages.
*/
queueSender.send(queueSession.createMessage());
} catch (JMSException e) {
System.out.println("Exception occurred: " +
e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {}
}
}
}
}






SimpleQueueReceive.java
/*
*
* Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
*
* This software is the proprietary information of Sun
* Microsystems, Inc. Use is subject to license terms.
*
*/
/**
* The SimpleQueueReceiver class consists only of a main method,
* which fetches one or more messages from a queue using
* synchronous message delivery. Run this program in conjunction
* with SimpleQueueSender. Specify a queue name on the command
* line when you run the program.
*/
import javax.jms.*;
import javax.naming.*;

public class SimpleQueueReceiver {

/**
* Main method.
*
* @param args the queue used by the example
*/
public static void main(String[] args) {
String queueName = null;
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueReceiver queueReceiver = null;
TextMessage message = null;

/*
* Read queue name from command line and display it.
*/
if (args.length != 1) {
System.out.println(”Usage: java ” +
“SimpleQueueReceiver “);
System.exit(1);
}
queueName = new String(args[0]);
System.out.println(”Queue name is ” + queueName);

/*
* Create a JNDI InitialContext object if none exists
* yet.
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println(”Could not create JNDI ” +
“context: ” + e.toString());
System.exit(1);
}

/*
* Look up connection factory and queue. If either does
* not exist, exit.
*/
try {
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup(”QueueConnectionFactory”);
queue = (Queue) jndiContext.lookup(queueName);
} catch (NamingException e) {
System.out.println(”JNDI lookup failed: ” +
e.toString());
System.exit(1);
}

/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create receiver, then start message delivery.
* Receive all text messages from queue until
* a non-text message is received indicating end of
* message stream.
* Close connection.
*/
try {
queueConnection =
queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queueReceiver = queueSession.createReceiver(queue);
queueConnection.start();
while (true) {
Message m = queueReceiver.receive(1);
if (m != null) {
if (m instanceof TextMessage) {
message = (TextMessage) m;
System.out.println(”Reading message: ” +
message.getText());
} else {
break;
}
}
}
} catch (JMSException e) {
System.out.println(”Exception occurred: ” +
e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {}
}
}
}
}




我不知道这个要怎么玩,可能要弄一个什么JNDI的配置,不过我没玩过,哪位大哥教教我啊!
我现在弄一个SAP的系统接口,它搞的很复杂,要涉及到很多东西,其中包括JMS消息机制,这块我没弄过,谁能教下我啊!
...全文
155 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wnba1983 2009-12-29
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 holly2k 的回复:]
JMS是需要有相应的容器的进行消息的处理的,需要有个服务器的,初始化InitialContext的时候要指向JMS服务器的地址,这两个类里并没有相应的内容,当然报错,先看一下JMS入门教程较好
[/Quote]
我现在就是不知道怎么配置数据源,还有,怎么把它发布到tomcat里面去
wnba1983 2009-12-29
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 holly2k 的回复:]
JMS是需要有相应的容器的进行消息的处理的,需要有个服务器的,初始化InitialContext的时候要指向JMS服务器的地址,这两个类里并没有相应的内容,当然报错,先看一下JMS入门教程较好
[/Quote]
你能给我个例子么,给个地址
holly2k 2009-12-29
  • 打赏
  • 举报
回复
JMS是需要有相应的容器的进行消息的处理的,需要有个服务器的,初始化InitialContext的时候要指向JMS服务器的地址,这两个类里并没有相应的内容,当然报错,先看一下JMS入门教程较好
crazylaa 2009-12-28
  • 打赏
  • 举报
回复
baidu+google,找些入门的资料看看。
tmaclc 2009-12-28
  • 打赏
  • 举报
回复
JMS 是需要导入相应的jar包才能运行的,你要用人家的src,当然要先导入啦,你可以找一个JMS入门的例子看看,直接在CSDN的博客里搜就可以找到了啊
wnba1983 2009-12-28
  • 打赏
  • 举报
回复
说没法实例化一个Factory
wnba1983 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zl3450341 的回复:]
引用 5 楼 wnba1983 的回复:
引用 3 楼 aspirehouse 的回复:
先运行SimpleQueueSender.java吧,它是发送者
再运行SimpleQueueReceive.java,它是接受者
不过直接Copy过来想一下运行正确不容易啊


我还是运行sender的时候就报异常了
就是这句话出错
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup(”QueueConnectionFactory”);


。。报什么异常呢  jar包没导入?
[/Quote]
导入了,异常报错
Could not create JNDI context: javax.naming.NoInitialContextException:Cannot instantiate class......
zl3450341 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wnba1983 的回复:]
引用 3 楼 aspirehouse 的回复:
先运行SimpleQueueSender.java吧,它是发送者
再运行SimpleQueueReceive.java,它是接受者
不过直接Copy过来想一下运行正确不容易啊


我还是运行sender的时候就报异常了
就是这句话出错
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup(”QueueConnectionFactory”);
[/Quote]

。。报什么异常呢 jar包没导入?
AspireHouse 2009-12-28
  • 打赏
  • 举报
回复
恩,去把EJB的JNDI和JMS以及MDB都看看
sebatinsky 2009-12-28
  • 打赏
  • 举报
回复
建议楼主去网上搜搜JMS可运行程序和流程,其实就是数据源配置
AspireHouse 2009-12-28
  • 打赏
  • 举报
回复
报的是Not bound?异常
wnba1983 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 aspirehouse 的回复:]
先运行SimpleQueueSender.java吧,它是发送者
再运行SimpleQueueReceive.java,它是接受者
不过直接Copy过来想一下运行正确不容易啊

[/Quote]
我还是运行sender的时候就报异常了
就是这句话出错
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup(”QueueConnectionFactory”);
zl3450341 2009-12-28
  • 打赏
  • 举报
回复
SimpleQueueSender.java
SimpleQueueReceive.java

你看这英语单词也知道啊
一个发送 一个接收
AspireHouse 2009-12-28
  • 打赏
  • 举报
回复
先运行SimpleQueueSender.java吧,它是发送者
再运行SimpleQueueReceive.java,它是接受者
不过直接Copy过来想一下运行正确不容易啊
wnba1983 2009-12-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 aspirehouse 的回复:]
在代码根文件夹下即src下写一个jndi.properties
内容:
  java.naming.provider.url = localhost:1099

  java.naming.factory.initial = org.jnp.interfaces.NamingContextFactory

[/Quote]
这个localhost后面的端口是什么意思啊,我是不是还需要tomcat之类的中间件什么环境啊,我现在什么都没做,就直接在Eclipse里面建了个工程,导了这2个java文件,要怎么运行啊!
AspireHouse 2009-12-28
  • 打赏
  • 举报
回复
在代码根文件夹下即src下写一个jndi.properties
内容:
java.naming.provider.url = localhost:1099

java.naming.factory.initial = org.jnp.interfaces.NamingContextFactory

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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