JBoss下EJB3.x简单环境搭建实例

1 篇文章 0 订阅
1 篇文章 0 订阅

一、环境说明

开发环境:Eclipse Java EE IDE for Web Developers  Version: Kepler Service Release 2

JBoss环境:jboss-as-7.1.1.Final

jdk版本:1.7.0

二、文档结构

	部分过程截图如下

2.1、在ejbModule目录下右键创建session Bean

2.2、为你的session Bean取一个名字,状态这里选择无状态(stateless)的session,并勾上Remote,它会自动帮助我们生成对应的接口


3、后续部分就开始写代码了

三、详细代码

3.1. HelloWorldBeanRemote接口代码

public interface HelloWorldBeanRemote {

	public String sayHello();
}

3.2. 无状态的HelloWorldBean 代码

import comjoe.bussiness.view.HelloWorldBeanRemote;
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless
@Remote(HelloWorldBeanRemote.class)
public class HelloWorldBean implements HelloWorldBeanRemote {

    public HelloWorldBean() {
    }

	@Override
	public String sayHello() {
		return "Hello world!";
	}

}

3.3. 客户端调试代码

import javax.naming.NamingException;
import com.joe.bussiness.view.HelloWorldBeanRemote;
import com.joe.clientutility.ClientUtility;

public class Client {

	public static void main(String[] args) {
		HelloWorldBeanRemote bean = doLookup();
		System.out.println(bean.sayHello());
	}

	private static HelloWorldBeanRemote doLookup() {
		HelloWorldBeanRemote bean = null;
		try {
			bean = ClientUtility.getInitialContext();
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return bean;
	}

}

3.4. 通过jndi方式查找对应的bean

package com.joe.clientutility;

import java.util.Hashtable;

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

import com.joe.bussiness.HelloWorldBean;
import com.joe.bussiness.view.HelloWorldBeanRemote;

public class ClientUtility {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static HelloWorldBeanRemote getInitialContext() throws NamingException {
		final Hashtable jndiProperties = new Hashtable();
		
		jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
		
		final Context context = new InitialContext(jndiProperties);
		
		final String appName = "";
		
		final String moduleName = "HelloWorldEJB";//工程名,这里写错会出现 IllegalStateException
		
		final String distinctName = "";
		
		final String beanName = HelloWorldBean.class.getSimpleName();
		
		final String viewClassName = HelloWorldBeanRemote.class.getName();
		
		System.out.println(moduleName);
		System.out.println(beanName);
		System.out.println(viewClassName);
		
		/**
		 * 要搞清楚这几个name的具体含义,可以参见如下论坛
		 * @see https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI?_sscc=t
		 */
		return (HelloWorldBeanRemote) context.lookup("ejb:"+appName+"/"+moduleName+"/"+
				distinctName+"/"+ beanName + "!"+viewClassName);
	}

}

3.5. 编写配置信息jboss-ejb-client.properties文件

endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false

remote.connections=default

remote.connection.default.host=localhost
remote.connection.default.port=4447
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

#remote.connection.default.username=username
#remote.connection.default.password=password

四、运行结果

直接运行client.java,结果截图如下


五、异常处理

刚开始因为对jndi查找bean中几个参数不了解,导致了如下异常

Exception in thread "main" java.lang.IllegalStateException: No EJB receiver available forhandling[appName:,modulename:HelloWorldEJB,distinctname:] combination for invocationcontextorg.jboss.ejb.client.EJBClientInvocationContext@196c67a

官方网站非常详细的说明了几个参数的含义,参考https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI?_sscc=t


  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值