InitialContext jboss中的异常

时间:2014-11-18 23:44:12

标签: java eclipse jboss

我正在使用jboss在java crawler中开发一个应用程序。我做了以下代码:

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设置。 一些帮助?

1 个答案:

答案 0 :(得分:1)

这应该有效

// 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