import java.util.Properties;
import javax.jms.*;
import javax.naming.*;
public class TopicConsumer implements MessageListener {
public static void main(String[] args) throws JMSException, NamingException {
System.out.println("------>STARTING JMS TopicConsumer<----");
Context context= TopicConsumer.getInitialContext();
System.out.println(">>>1<<<");
TopicConnectionFactory topicConnectionFactory= (TopicConnectionFactory)context.lookup("ConnectionFactory");
System.out.println(">>>2<<<");
Topic topic= (Topic) context.lookup("Topic/AKSHAY");
System.out.println(">>>3<<<");
TopicConnection topicConnection= topicConnectionFactory.createTopicConnection();
System.out.println(">>>4<<<");
TopicSession topicSession= topicConnection.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
System.out.println(">>>5<<<");
topicSession.createSubscriber(topic).setMessageListener(new TopicConsumer());
System.out.println(">>>6<<<");
topicConnection.start();
System.out.println("------>Énding JMS TopicConsumer<----");
}
@Override
public void onMessage(Message message) {
try {
System.out.println(">>>on msg<<<");
System.out.println("Incoming message is"+((TextMessage)message).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
public static Context getInitialContext() throws JMSException, NamingException{
System.out.println(">>>7<<<");
Properties props=new Properties();
System.out.println(">>>8<<<");
props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
System.out.println(">>>9<<<");
props.setProperty("java.naming.factory.url.pkgs","org.jboss.naming");
System.out.println(">>>10<<<");
props.setProperty("java.naming.provider.url","localhost:1099");
System.out.println(">>>11<<<");
Context context= new InitialContext();
System.out.println(">>>12<<<");
return context;
}
}
线程“main”中的异常javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或者在applet参数中或在应用程序资源文件中指定类名:java.naming.factory.initial 在javax.naming.spi.NamingManager.getInitialContext(未知来源) 在javax.naming.InitialContext.getDefaultInitCtx(未知来源) 在javax.naming.InitialContext.getURLOrDefaultInitCtx(未知来源) 在javax.naming.InitialContext.lookup(未知来源) 在TopicProducer.main(TopicProducer.java:19)
我是Jms的新手。我的代码就像这样。异常是命名异常: 我似乎在设置JNDI或类路径时遇到问题。 我已经包含以下JMS.jar文件 类路径或JNDI中的问题......? 使用Eclipse Juno Jboss 5.0和JDK 1.7
答案 0 :(得分:0)
您可能忘记在创建上下文时包含属性。
所以而不是
Context context= new InitialContext();
试
Context context= new InitialContext(properties);