为何在JBoss中发布Message-Driven Bean总出Exception in thread "main" javax.naming.Nam

03-03-31 chenjianxiong

LogBean.java

import javax.ejb.*;

import javax.jms.*;

public class LogBean implements MessageDrivenBean, MessageListener {

protected MessageDrivenContext ctx;

public void setMessageDrivenContext(MessageDrivenContext ctx) {

this.ctx = ctx;

}

public void ejbCreate() {

System.err.println("ejbCreate()");

}

public void onMessage(Message msg) {

TextMessage tm = (TextMessage) msg;

try {

String text = tm.getText();

System.err.println("Received new message : " + text);

}

catch(JMSException e) {

e.printStackTrace();

}

}

public void ejbRemove() {

System.err.println("ejbRemove()");

}

}

*************************************************************

TestClient.java

import javax.naming.*;

import javax.jms.*;

import java.util.*;

public class TestClient {

public static void main (String[] args) throws Exception {

// Initialize JNDI

Context ctx = new InitialContext(System.getProperties());

// 1: Lookup ConnectionFactory via JNDI

TopicConnectionFactory factory =

(TopicConnectionFactory)

ctx.lookup("TopicConnectionFactory");

// 2: Use ConnectionFactory to create JMS connection

TopicConnection connection =

factory.createTopicConnection();

// 3: Use Connection to create session

TopicSession session = connection.createTopicSession(

false, Session.AUTO_ACKNOWLEDGE);

// 4: Lookup Desintation (topic) via JNDI

Topic topic = (Topic) ctx.lookup("topic/testtopic");

// 5: Create a Message Producer

TopicPublisher publisher = session.createPublisher(topic);

// 6: Create a text message, and publish it

TextMessage msg = session.createTextMessage();

msg.setText("This is a test message.");

publisher.publish(msg);

}

}

**********************************************************************

jboss.xml

<?xml version="1.0"?>

<jboss>

<enterprise-bean>

<message-driven>

<ejb-name>Log</ejb-name>

<destination-jndi-name>topic/testtopic</destination-jndi-name>

<resource-ref>

<res-ref-name>TopicConnectionFactory</res-ref-name>

<res-type>javax.jms.TopicConnectionFactory</res-type>

</resource-ref>

</message-driven>

</enterprise-bean>

</jboss>

**************************************************************

ejb-jar.xml

<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/j2ee/dtds/ejb-jar_2_0.dtd">

<ejb-jar>

<enterprise-beans>

<message-driven>

<ejb-name>Log</ejb-name>

<ejb-class>LogBean</ejb-class>

<transaction-type>Container</transaction-type>

<message-driven-destination>

<destination-type>javax.jms.Topic</destination-type>

</message-driven-destination>

</message-driven>

</enterprise-beans>

</ejb-jar>

*****************************************************************

环境信息jndi.properties

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

java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

java.naming.provider.url=localhost