简体   繁体   English

EJB-将数据发送到远程

[英]EJB - Send Data to remote

I want to exchange data between two applications JEE6/JSF2.0 and i'm looking for the best solution. 我想在两个应用程序JEE6 / JSF2.0之间交换数据,我正在寻找最佳解决方案。 I thought of the below solutions : 我想到了以下解决方案:

  • by using a JSON file. 通过使用JSON文件。
  • by using XML file. 通过使用XML文件。
  • by using GSON file. 通过使用GSON文件。
  • by using Remote interface (EJB 3.0). 通过使用远程接口(EJB 3.0)。

For you, what's the best solution to use ? 对您来说,最佳的解决方案是什么?

edit : This two applications will be always running on the same network (but can not be on the same JVM) 编辑:这两个应用程序将始终在同一网络上运行(但不能在同一JVM上)

I want to provide an alternative to David's answer, as I feel that there are some drawbacks to RMI that he underplayed. 我想提供一种替代David答案的方法,因为我认为RMI有一些缺点,他没有充分利用。

  1. This is a Java specific technology. 这是Java特定的技术。 If a third server needs to be introduced and it is a Microsoft Reporting Services server for example, then it cannot talk in the same language. 如果需要引入第三台服务器,例如它是Microsoft Reporting Services服务器,则它不能使用相同的语言进行通话。

  2. RMI is an OLD technology and doesn't particularly look well on a CV. RMI是一种过时的技术,在CV上看起来并不是特别好。 Web services are the future. Web服务是未来。 Experienced RMI developers are more uncommon than experienced web service developers. 有经验的RMI开发人员比有经验的Web服务开发人员更为罕见。

  3. Cumbersome and heavy framework 繁琐而沉重的框架

A better solution in my opinion would be to use SOAP XML based web services. 我认为更好的解决方案是使用基于SOAP XML的Web服务。 Here are some advantages to this approach: 这种方法有一些优点:

  1. Universal acceptance in nearly any development framework. 几乎在任何开发框架中都被普遍接受。 No matter the technology, nearly all have helpful libraries for interacting with web services. 无论采用哪种技术,几乎所有人都具有与Web服务交互的有用库。

  2. Java has good support for object serialization into XML. Java对将对象序列化为XML有很好的支持。 This means objects can be quickly serialized into a SOAP XML request, sent to the other server, and deserialized back into a Java object by the other application server for processing. 这意味着可以将对象快速序列化为SOAP XML请求,发送给其他服务器,然后由其他应用程序服务器反序列化为Java对象进行处理。

  3. A service layer can give you the decoupling interface between the two applications just as RMI can. 服务层可以像RMI一样为您提供两个应用程序之间的解耦接口。

I hope you reconsider the use of SOAP XML based web services in your application. 希望您重新考虑在应用程序中使用基于SOAP XML的Web服务。

There's two options really as you yourself stated. 正如您自己所说的,有两种选择。

Using RMI to connect to a EJB or using a webservice and communicating by JSON/XML etc... 使用RMI连接到EJB或使用Web服务并通过JSON / XML等进行通信...

From my experience RMI can be favorable if your applicaitons are on the same network, if not then you might get problems with firewalls etc and be forced to tunnel the RMI using HTTPS... which pretty much makes the RMI calls webservice calls. 根据我的经验,如果您的应用程序位于同一网络上,则RMI可能会很有利,否则,您可能会遇到防火墙等问题,并被迫使用HTTPS隧道传输RMI ...这几乎使RMI调用了网络服务调用。

If your on two different machines then webservices are nice as they dont cause as much trouble with firewalls. 如果您使用的是两台不同的计算机,那么Web服务就不错了,因为它们不会给防火墙造成太多麻烦。 Also as they use the HTTP protocol you dont have to worry about the data being transfered. 同样,由于他们使用HTTP协议,因此您不必担心正在传输的数据。

These examples are kinda generalised but should give you some insight. 这些示例有些笼统,但应该可以使您有所了解。

GSON vs XML vs JSON is a completely different subject... Non is superiour to the other, and all are fairly easily read by the human eye. GSON vs XML vs JSON是一个完全不同的主题... Non优于其他,而且人眼都可以轻松阅读所有内容。

UPDATE From what I've understod you wont have to worry about firewalls and such, I would recommend using RMI. 更新从我了解的内容来看,您不必担心防火墙,因此,我建议您使用RMI。 It usually results in cleaner code and somewhat better performance. 通常,它可以使代码更简洁,性能也更好。

Since I have seen both in action, I can make a comparison between the two technologies, EJB and WebServices. 既然我已经看到了两者的实际应用,那么我可以在EJB和WebServices这两种技术之间进行比较。 I can confirm that EJB is way more efficient, has support of transactions (including distributed transactions, if that is your requirement), exception handling, and binary streaming out of the box. 我可以确认EJB效率更高,支持事务(包括分布式事务,如果需要的话),异常处理和开箱即用的二进制流。 In terms of performance EJB may exceed SOAP by a factor of 5 times in speed, and REST for about 3 times. 在性能方面,EJB的速度可能比SOAP高出5倍,而REST大约是SOAP的3倍。

However, EJB is not an integration technology. 但是,EJB不是集成技术。 In fact, it has never thought to do so. 实际上,它从未想过这样做。 The biggest flaw of EJB is that it is very coupled to the Java Platform. EJB的最大缺陷是它与Java平台非常耦合。 Therefore, both endpoints must be written in Java and should use the same Java EE version. 因此,两个端点都必须用Java编写并且应使用相同的Java EE版本。

Another problem is that EJB is not a protocol per se, so the implementations from two containers/vendors is probably different. 另一个问题是EJB本身不是协议,因此来自两个容器/供应商的实现可能有所不同。 If you need to access a remote EJB from JBoss AS on an Oracle WebLogic server, you must bring JBoss EJB client implementation with you. 如果需要从Oracle WebLogic服务器上的JBoss AS访问远程EJB,则必须随身携带JBoss EJB客户端实现。

Another big problem related to integration with EJB is a lack of data exchange format. 与EJB集成相关的另一个大问题是缺乏数据交换格式。 Since it uses Java Serialized objects for communication, the data types must be shared on both ends. 由于它使用Java序列化对象进行通信,因此数据类型必须在两端共享。 If you create a new exception type on the server that is classified as an Application Exception, if the client who consumes this service triggers the exception, his code will break. 如果您在分类为应用程序异常的服务器上创建新的异常类型,则使用此服务的客户端触发该异常时,其代码将中断。 Note that, in this case the remote API was not violated, but another unknown type was introduced. 请注意,在这种情况下,不会违反远程API,但是引入了另一个未知类型。

And, of course, by depending solely on the class type as an exchange format, you are giving the programmers opportunity for doing very stupid things. 而且,当然,通过仅依赖于类类型作为交换格式,就为程序员提供了做非常愚蠢的事情的机会。 If you have many different teams in large projects using EJB as integration technology using different versions of Java EE, prepare yourself to experience uttermost pain. 如果您在使用EJB作为集成技术(使用不同版本的Java EE)的大型项目中拥有许多不同的团队,请做好充分的准备。 I've seem a programmer including a JPA entity on the client, who was annotated with named queries, the table which was accessing, its columns, etc, essentially giving away all the database layout to the service consumer. 我似乎是一个程序员,包括在客户端上的JPA实体,该实体使用命名查询,正在访问的表,其列等进行注释,实质上是将所有数据库布局分配给了服务使用者。 But it can get even worse. 但情况可能更糟。 I've already seem a programmer returning a data structure that belonged to a dependency, namely Eclipselink 1.0. 我似乎已经在程序员返回属于依赖项的数据结构,即Eclipselink 1.0。 However, if you access this from a JBoss server, Eclipselink is also a JPA implementation technology, which conflicts with JBoss' hibernate. 但是,如果您从JBoss服务器访问它,则Eclipselink也是JPA实现技术,它与JBoss的休眠状态冲突。 So, now you have to include Eclipselink jar in your JBoss APP classpath and configure the container for not loading JPA related packages, which otherwise will break your application completely. 因此,现在您必须在JBoss APP类路径中包含Eclipselink jar,并配置该容器以不加载与JPA相关的软件包,否则将完全破坏您的应用程序。 Even so, it can get WORSE than before: some other service you need to connect had also the bright idea of using the same datastructure, but now from Eclipselink 1.1.1, that has a different implementation, but the same class signature. 即使这样,它也可以比以前得到WORSE:需要连接的其他一些服务也有使用相同数据结构的好主意,但是从Eclipselink 1.1.1开始,它具有不同的实现,但是具有相同的类签名。 Now you are in a very bad situation. 现在您的处境非常糟糕。

The bottom line: NEVER, EVER, use EJB as an integration technology. 最重要的是:永远不要将EJB用作集成技术。 Use SOAP using a contract-first approach, where you define a canonical data model for the application, mapping java datastructures to a XML exchange format that can be used by any client, be it written in any language or using different stacks. 使用基于合同优先的方法的SOAP,在该方法中,您可以为应用程序定义规范的数据模型,将Java数据结构映射为XML交换格式,该格式可以由任何客户端使用,无论是以任何语言编写还是使用不同的堆栈编写。 Or use REST implementing a resource based, using HATEOAS principles. 或使用HATEOAS原理使用REST实现基于资源的资源。 These days I rarely seem a reason for using EJB at all, since CDI is now on the market, support many features that EJB does and does not include any RPC related technology. 这些天来,我似乎几乎根本就没有使用EJB的理由,因为CDI现在已经上市,它支持EJB所具有的许多功能,并且不包括任何与RPC相关的技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM