ejb 远程调用

1,客户端代码:

package com.example.test;

import java.util.Hashtable;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.example.HelloWorldRemote;

//import cn.ejb.HelloWorldRemote;

public class HelloWorldTest {
    public static void main(String[] args) {
        Properties jndiProperties = new Properties();
//        Hashtable jndiProperties = new Hashtable();
        jndiProperties.put("jboss.naming.client.ejb.context", true);
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        
        //写这里不行!!!
//        jndiProperties.put(Context.SECURITY_PRINCIPAL, "testUser");
//        jndiProperties.put(Context.SECURITY_CREDENTIALS, "12345678");
        
        
        try {
            Context context = new InitialContext(jndiProperties);
            final String appName = "";
            final String moduleName = "hello";
            final String distinctName = "";

            Object obj = context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/HelloWorld!com.example.HelloWorldRemote");
            System.out.println(obj);
            HelloWorldRemote hwr = (HelloWorldRemote) obj;
            String say = hwr.hello( );
            System.out.println(say);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}

2,src下的jboss-ejb-client.properties

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connections=default
remote.connection.default.host=211.100.75.242
remote.connection.default.port = 4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.username=testUser
remote.connection.default.password=12345678

 3,用jboss\bin\add-user.sh 加个 applicationRealm

[userone@localhost bin]$ ./add-user.sh


What type of user do you wish to add?

a) Management User (mgmt-users.properties)

b) Application User (application-users.properties)

(a): b



Enter the details of the new user to add.

Realm (ApplicationRealm) :  ApplicationRealm ---->> Careful Here . You need to type this or leave it blank . I filled an incorrect value here and things went wrong from there .

Username : testuser

Password : testpassword

Re-enter Password : testpassword



What roles do you want this user to belong to? (Please enter a comma separated list, or leave blank for none) : testrole

About to add user 'testuser' for realm 'ApplicationRealm'



Is this correct yes/no? yes



Added user 'testuser' to file '/home/userone/jboss-as-7.1.0.Final/standalone/configuration/application-users.properties'

Added user 'testuser' to file '/home/userone/jboss-as-7.1.0.Final/domain/configuration/application-users.properties'

Added user 'testuser' with roles testrole to file '/home/userone/jboss-as-7.1.0.Final/standalone/configuration/application-roles.properties'

Added user 'testuser' with roles testrole to file '/home/userone/jboss-as-7.1.0.Final/domain/configuration/application-roles.properties'

.
大功告成了
参考: .http://stackoverflow.com/questions/19129836/javax-security-sasl-saslexception-authentic-failed-while-connecting-to-jboss-7
http://middlewaremagic.com/jboss/?p=1466
https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
posted @ 2015-09-18 14:47  Bigben  阅读(298)  评论(0编辑  收藏  举报