发帖    主题    评论    推荐    标签    作者    订阅    查搜    注册   登陆   关注
 
面向对象 设计模式 领域驱动设计 企业架构 框架 开发教程 微服务 CQRS 扩展性 并发编程 事件溯源 分布式 SOA

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

2003-03-31 22:01
赞助商链接

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


赞助商链接

赞助商链接

返回顶部

移动版 关于本站 使用帮助 联系管理员 最佳分辨率1366x768
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-20 jdon.com