`
yuan1129
  • 浏览: 2785 次
文章分类
社区版块
存档分类
最新评论

Calling EJB3 deployed in Websphere AS7 from Standalone Java client

阅读更多
1、先编写EJB3:

业务接口FirstEJBService
package com.first;

public interface FirstEJBService {
 void print(String msg);
}


EJB Local接口FirstEJBServiceBeanLocal
package com.first;

import javax.ejb.Local;

@Local
public interface FirstEJBServiceBeanLocal extends FirstEJBService {
}


EJB Remote接口FirstEJBServiceBeanRemote
package com.first;

import javax.ejb.Remote;

@Remote
public interface FirstEJBServiceBeanRemote extends FirstEJBService {
}


业务实现类FirstEJBServiceBean
package com.first;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless(mappedName = "FirstEJBServiceBean")
@Local({ FirstEJBServiceBeanLocal.class })
@Remote({ FirstEJBServiceBeanRemote.class })
public class FirstEJBServiceBean implements FirstEJBService {

	@Override
	public void print(String msg) {
		System.out.println("FirstEJBService echo : " + msg);
	}

}


2、打包成jar后,发布到Websphere中;

3、查看JNDI
在cmd下调用WebSphere\AppServer\bin目录中dumpNameSpace.bat,可以查看到所有的JNDI,找到该EJB对应的JNDI全称;

4、编写Standalone Java client
package com.ejbclient.first;

import java.util.Properties;

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

import com.first.FirstEJBService;

public class FirstEJBClient {

	/**
	 * @param args
	 * @throws NamingException
	 */
	public static void main(String[] args) throws NamingException {
		Properties env = new Properties();
		env.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.ibm.websphere.naming.WsnInitialContextFactory");
		env.put(Context.PROVIDER_URL, "iiop://localhost:2809");
		InitialContext context = new InitialContext(env);

		Object result = context
				.lookup("ejb/FirstEJB/FirstEJB.jar/FirstEJBServiceBean#com.first.FirstEJBServiceBeanRemote");

		FirstEJBService service = (FirstEJBService) javax.rmi.PortableRemoteObject
				.narrow(result, FirstEJBService.class);

		service.print("Hello EJB");
	}

}



client中,需要com.ibm.websphere.naming.WsnInitialContextFactory类,该类存在于WebSphere\AppServer\runtimes目录下的com.ibm.ws.admin.client_7.0.0.jar中,运行的时候需要在classpath中加上该jar;

到这并没有结束,由于Client与EJB不在同一个JVM中,所以还需要调用WebSphere\AppServer\bin目录中createEJBStubs.bat创建EJB stub:
createEJBStubs.bat com.first.FirstEJBService  -cp .


在运行的时候,将createEJBStubs.bat产生的_FirstEJBService_Stub.class放入classpath中,否则会产生异常:
java.lang.ClassCastException: org.omg.stub.java.rmi._Remote_Stub incompatible with com.first.FirstEJBService

或者
Exception in thread "P=556551:O=0:CT" java.lang.ClassCastException: Unable to load class: com.first._FirstEJBService_Stub
分享到:
评论

相关推荐

    Calling R from Java

    3. **隐藏R对象作为Java对象**:最理想的方式是通过上述的“外键”机制将R对象包装成Java对象,这可以让其他代码只依赖于这个Java对象,而不是直接依赖于R对象。这样一来,即使将来R的实现有所改变,只要Java对象的...

    Calling Java Class Methods from C with JNI

    标题:"Calling Java Class Methods from C with JNI" 解释了如何使用Java本地接口(JNI)技术来从C语言代码中调用Java类的方法。JNI是Java提供的一种标准编程接口,允许Java代码和其他语言写的代码进行交互。这在...

    method-calling.rar_in

    这个名为"method-calling.rar_in"的压缩包文件包含了一个关于Java方法调用的30个示例,旨在帮助学习者深入理解和掌握这一关键概念。以下是关于Java方法调用的一些详细知识点: 1. **方法定义**:在Java中,方法是一...

    Calling C from IDL(2002)

    《Calling C from IDL》是2002年发布的一份文档,主要探讨了如何在IDL(Interactive Data Language)环境中调用C和C++代码。IDL是一种强大的数据处理和可视化语言,广泛应用于科学计算和数据分析领域。而C和C++是...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Calling a C Function from a Java Program 936 Numeric Parameters and Return Values 942 String Parameters 944 Accessing Fields 950 Encoding Signatures 954 Calling Java Methods 956 Accessing Array ...

    Java邮件开发Fundamentals of the JavaMail API

    Currently in version 3, also known as POP3, RFC 1939 defines this protocol. POP is the mechanism most people on the Internet use to get their mail. It defines support for a single mailbox for each ...

    Jess__The_Java_Expert_System_Shell

    Jess__The_Java_Expert_System_Shell:DistributionCategory UC-411 SAND98-8206 (revised)Unlimited... A guide to calling Java functions from Jess, and to extending Jess by writing Java code, is alsoincluded.

    javacv-platform-1.3.3-src

    JavaCV uses wrappers from the JavaCPP Presets of commonly used libraries by researchers in the field of computer vision (OpenCV, FFmpeg, libdc1394, PGR FlyCapture, OpenKinect, librealsense, CL PS3 Eye...

    Calling C Programs from IDL

    2. **书籍推荐**:Ronn Kling撰写的《Calling C from IDL: Using DLM's to Extend Your IDL Code》是一本权威指南,书中包含了大量示例代码和实践经验,对于希望深入学习IDL与C交互的技术人员来说是非常宝贵的资源。...

    Clojure调用Java类的一个实例

    (require '[clojure.java.jdk :as jdk]) (def hello-world (jdk/class-for-name "com.example.HelloWorld")) (let [instance (hello-world)] (.sayHello instance)) ``` 在上述代码中,`clojure.java.jdk`是一个...

    Microsoft Teams Calling Solutions Calling Solutions.pdf

    Microsoft Teams Calling Solutions 知识点 Microsoft Teams Calling Solutions 是微软公司的一项电话解决方案,旨在帮助企业和中小型企业(SMB)提高通信效率,降低成本。根据 Forrester 的研究报告,本解决方案...

    Calling MATLAB in Julia through MATLAB Engine.zip

    这个主题"Calling MATLAB in Julia through MATLAB Engine"探讨的就是如何在Julia编程语言中利用MATLAB Engine接口来实现这种跨环境的计算。下面将详细阐述这一技术的背景、原理以及实施步骤。 首先,MATLAB Engine...

    Python库 | joint-calling-0.1.88.tar.gz

    标题中的"Python库 | joint-calling-0.1.88.tar.gz"表明这是一个与Python相关的库,且版本号为0.1.88,它被打包成一个tar.gz文件。这种格式通常用于在Unix/Linux环境中分发软件,因为它可以进行文件压缩并合并多个...

    [3planesoft屏幕保护程序合集].Premium.3D.Screensavers.iso

    At one of the most famous battleships in history, who served from World War II and ending with "a storm in the desert! Consider the battleship �Mighty Mo� and enjoy the amazingly detailed animation ...

    Calling Conventions Topics

    ### Calling Conventions in C/C++ #### 概述 在 C 和 C++ 编程语言中,函数调用约定(Calling Conventions)定义了如何在函数调用时传递参数、返回值以及清理调用栈的方式。不同的调用约定适用于不同的场景,并且...

    J/Invoke 2010 jinvoke jni 天涯浪子

    Unlike error-prone JNI programming that is hard to use and deploy, J/Invoke lets you access native libraries by simply declaring native methods, and calling them from pure Java. Java programmers can ...

    ARASHI calling 歌词

    《ARASHI calling》是日本流行音乐团体ARASHI的一首歌曲,歌词中充满了积极向上的力量和对生命的执着追求。这首歌的主题围绕着把握现在、永不放弃和自我信念展开。 歌词中的“Calling”象征着呼唤,可能是对生活、...

    arm smc calling convention

    - 从安全模式返回非安全模式时,使用`ret_from_smc`指令完成。 #### 应用场景 - **启动安全服务**:例如,加密解密操作、密钥管理等。 - **访问硬件资源**:如访问可信平台模块(TPM)、加密引擎等。 - **执行...

    微软内部资料-SQL性能优化3

    Lesson 1: Concepts – Locks and Lock Manager 3 Lesson 2: Concepts – Batch and Transaction 31 Lesson 3: Concepts – Locks and Applications 51 Lesson 4: Information Collection and Analysis 63 Lesson 5:...

Global site tag (gtag.js) - Google Analytics