gpt4 book ai didi

java - InitialContext jboss 中的异常

转载 作者:行者123 更新时间:2023-11-30 11:12:12 25 4
gpt4 key购买 nike

我正在使用 jboss 在 java 爬虫中开发一个应用程序。我编写了以下代码:

connectionFactoryLookupAddress = new String("jms/RemoteConnectionFactory");
destinationLookupAddress = new String("jms/topic/mC");
environment = new Properties();
environment.put(Context.SECURITY_PRINCIPAL, "isuser");
environment.put(Context.SECURITY_CREDENTIALS, "is");
environment.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.remote.client.InitialContextFactory");
environment.put(Context.PROVIDER_URL, "remote://localhost:4447");
InitialContext iCtx = new InitialContext(environment);

我得到以下异常

javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.naming.remote.client.InitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory]
(...)
Caused by: java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory
(...)

已经添加了 jboss-client.jar,但没有...非得配置jboss服务器打这个项目或者在服务器上添加用户?我正在使用 eclipse 。我已经阅读了一些关于这个主题的主题,但我不清楚运行这段代码所需的 jboss 设置。有帮助吗?

最佳答案

这是应该的

// Set up all the default values
private static final String DEFAULT_MESSAGE = "Hello, World!";
private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
//private static final String DEFAULT_DESTINATION = "paymentNotification";
private static final String DEFAULT_DESTINATION = "myTopic";
private static final String DEFAULT_MESSAGE_COUNT = "5";
private static final String DEFAULT_USERNAME = "jms";
private static final String DEFAULT_PASSWORD = "jms";
private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
private static final String PROVIDER_URL = "http-remoting://127.0.0.1:8080";
//https://github.com/wildfly/quickstart/tree/master/helloworld-jms
public static void main(String[] args) {

Context namingContext = null;

try {
String userName = DEFAULT_USERNAME;
String password = DEFAULT_PASSWORD;

// Set up the namingContext for the JNDI lookup
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
env.put(Context.SECURITY_PRINCIPAL, userName);
env.put(Context.SECURITY_CREDENTIALS, password);
namingContext = new InitialContext(env);

// Perform the JNDI lookups
String connectionFactoryString =DEFAULT_CONNECTION_FACTORY;
System.out.println("@@Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup(connectionFactoryString);
System.out.println("@@@Found connection factory \"" + connectionFactoryString + "\" in JNDI " +connectionFactory.toString());

String destinationString = System.getProperty("destination", DEFAULT_DESTINATION);
System.out.println("@@Attempting to acquire destination \"" + destinationString + "\"");
Destination destination = (Destination) namingContext.lookup(destinationString);
System.out.println("@@@Found destination \"" + destinationString + "\" in JNDI");

int count = Integer.parseInt(System.getProperty("message.count", DEFAULT_MESSAGE_COUNT));
String content = DEFAULT_MESSAGE;

try (JMSContext context = connectionFactory.createContext(userName, password)) {
System.out.println("###Sending " + count + " messages with content: " + content);
// Send the specified number of messages


for (int i = 0; i < count; i++) {
context.createProducer().send(destination, content);
}

同时在configuration-full.xml中配置jms主题 Link

关于java - InitialContext jboss 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27006345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com