(Java) 如何将模式验证的 XML 文档作为参数在分布式组件(例如 Web 服务或套接字)之间传递?

发布于 2024-09-16 02:10:40 字数 2081 浏览 11 评论 0原文

以下是该场景的描述,如果对所使用的方法有任何评论,我也将不胜感激。

我的应用程序的核心是一组由 P2P 数据库支持的 Web 服务。一项服务接受一个简单的基于 XML 的记录(我为其设计了一个通用架构)。该服务处理该数据(主要是根据特定标准创建密钥)并将原始数据与创建的密钥一起传递到侦听 P2P 节点之一中的侦听 SocketServer。该键、数据对被路由到正确的节点,该节点将数据(与作为 ID 的键相关联)存储在 XML 数据库中。

第二个服务接受基于相同架构构建的查询文档,但具有可用于从先前存储的文档中进行搜索和匹配的可选值。因此,第二个服务会将此查询(使用正确的密钥)传递给 P2P 部分,获取结果并将其传递回服务客户端。

例如,如果提交给第一个服务的原始记录<;属性1>值1< /attr1> <属性2>值2 < /属性2> (属性列表以及模式规定的一些其他元数据),那么如果收到的查询<<,第二个服务应该检索该记录。属性2>值2< /属性2>

(我稍后可以考虑使用更复杂的 XPath 或 XQuery 查询,因为底层 XML 数据库允许而不是对此处的值进行精确匹配,但这在现阶段并不重要。我正在开发第三项服务,但它取决于获取首先,前两个形状正确)

所以我的问题是:

1)我应该使用什么数据类型作为 Web 服务的参数?如何利用我的架构来实现此用途?我正在考虑为此使用各种 XML 绑定框架(尤其是 JAXB 和 SDO),但不知道如何继续。

2)如何增强这两个服务(称为存储和搜索)以使用基于原始通用模式动态创建的模板?该服务仍然接受主模式类型的文档,但具有基于模板的内部属性列表,例如 template1 仅需要其值为整数,而 template2 需要(浮点)和(字符串)。当前基于 JSP 的原型手动创建此模板,但作为手动组装的 XML 文档(<>标签分散在文本中)并且没有类型检查,所以我认为我可以做得更好!

3)是否可以生成一个快速的 Web 应用程序原型,以便简单地访问该系统(再次使用架构(和模板)来编辑适当的 XML 消息结构?我正在寻找的是(人类)用户选择一个模板,然后只需“填空”并提交,不需要任何花哨的外观和感觉

4) 我可以或如何使用此 XML 消息类型进行跨套接字通信?

5) 我是否将服务部署为无状态 EJB 有关系吗?我是否需要将它们作为 EJB 或 servlet 就足够了?

我目前有一个基本的实现(来自以前的开发人员),旨在满足我当前需求的子集(我正在改进服务并添加新的派生服务),但没有模式也没有验证,并且数据一直作为基本传递字符串,从而提供弱类型并且难以更新手动解析。我想将其更新为更强的绑定类型的原因是引入数据模式的更改,这些更改可以轻松地沿着整个系统传递。基本上,我希望系统尽可能少地与所使用的数据格式/模式耦合;当前的原型与数据的耦合度太高,我发现在不破坏系统的情况下更改数据非常困难。

我的初步调查使我考虑使用 JAXB,但它仅支持静态类型(无法在运行时动态创建我想要保留以供以后使用的模式/类型)。所以我遇到了 SDO,它同时具有动态和静态类型。问题在于没有足够的社区和/或使用这种方法的示例,因此它似乎有风险(Apache Tuscany 和 Eclipselink 实现的示例非常稀缺,我找不到 5 年以上历史的完整示例(例如这个 http://www.ibm.com/developerworks/java/library/ j-sdo/),并且还解决了 SDO 的 XML 用例(大多数似乎关注 SDO 的关系用法)。

这是我第一次寻求编程帮助(在这里和其他地方),所以请耐心等待。我在网上搜索了很多,但我找不到任何有用的东西,但到处都没有添加

任何评论或提示,我真的很感激

trfndr

编辑

我忘了一件事:如何。搜索服务返回结果?由于它打开客户端套接字连接,因此无法同步返回任何结果,当前的实现通过让服务客户端在随机端口上打开侦听套接字并放置此联系人来解决此问题。查询文档中的信息。搜索 Web 服务将查询发送到 p2p 部分后,它就完成了。 p2p 将结果作为 WS 调用发送到另一个服务,该服务将结果发送回服务客户端套接字。我不太喜欢这种方法,有没有更优雅的解决方案?

Here is a description of the scenario and I would appreciate also any comments on the approach used

The core of my application is a set of web services backed by a P2P database. One service accepts a simple XML-based record (I have designed a generic schema for it). The service processes this data (mainly creating keys based on certain criteria) and pass the original data along with the created keys to a listening SocketServer in one of the listening P2P nodes. This key,data pair is routed to the proper node, which stores the data (associated with the key as an ID) in an XML database.

A second service accepts a query document that is structured based on the same schema, but with optional values that would be used for searching and matching from the previously stored ones. So the second service would pass this query (with the proper keys) to the P2P part, get back the results and pass them back to the service client.

E.g. if the original record submitted to the first service was < attr1 >value1 < /attr1 > < attr2 > value2 < / attr2 > (attribute list along with some other metadata mandated by the schema) then the second service should retrieve that record if the query received was < attr2 >value2 < / attr2 >

(I could later think about using more complex XPath or XQuery queries as the underlying XML database allows instead of exact matches for values here but that is not important at this stage. there is also a third service I am working on but it depends on getting the first two in proper shape first)

So my questions are:

1) What data type should I use as the parameters of the web services? How to utilize my schema for this usage? I was considering various XML binding frameworks (especially JAXB and SDO) for this but didn't know how to proceed.

2) How can I enhance the two services (call them store and search) to use dynamically created templates based on the original generic schema? The service would still accept documents of the main schema type but has the inner attribute list based on a template say template1 only requires whose values are ints while template2 require (float) and (string). The current JSP-based prototype manually creates this template but as an XML document that is assembled by hand (<>tags dispersed in text) and there is no type checking what so ever so I thought I could do better!

3) Is it possible to generate a quick web app prototype for simple access to this system (again by using the schema (&templates) to edit the appropriate XML message structures? What I am looking for is for the (human) user to choose a template and then just "fill in the blanks" and submit, no need for any fancy look and feel.

4) Can I or how can I also use this XML message type for communicating across sockets?

5) Does it matter if I deploy the services as stateless EJBs or not? Do I need them to be EJBs or servlets would be more than enough?

I currently have a rudimentary implementation (from previous developers) that were meant for a subset of my current requirements (I am improving on the services and adding new derived ones) but there was no schema nor validation and the data is passed all along as basic strings, thus providing weak typing and difficult to update manual parsing. The reason I want to update this to a stronger bound typing is to introduce changes in the data schema that would be passed along the whole system easily. Basically I want the system to be as less coupled to the data format/schema used as possible; the current prototype is too coupled to the data that I am finding it extremely difficult to change the data without breaking the system.

My initial investigation led me to consider JAXB but it supports only static typing (cannot create a schema/types dynamically at runtime that I want to persist for later usage). So I came across SDO which has both dynamic and static typing. The problem is just that there is not enough community and/or examples of using this approach so it seems risky (the examples of Apache Tuscany and Eclipselink implementations are very scarce and I could not find complete examples that are not 5+ years old (like this http://www.ibm.com/developerworks/java/library/j-sdo/) and also tackles the XML use case of SDO (most seem to focus on the relational usage of SDO).

This is my first time asking for programming help (here and elsewhere) so please bear with me. I searched a lot on the net but I could not find anything useful but pieces here and there that did not add up.

Any comment or hint is really appreciated.

trfndr

EDIT

I forgot one thing: how would the search service get back the results? Since it is opening a client socket connection, there is no way to get back any results synchronously. The current implementation tackles this by having the service client opening a listening socket on a random port and putting this contact info in the query document. After the search web service sends the query to the p2p part it finishes. The p2p sends the results as a WS call to another service which sends them back to the service client socket. I don't like this approach much, is there any more elegant solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦忆晨望 2024-09-23 02:10:40

我领导 EclipseLink JAXB & SDO 实现并代表 Oracle 遵守这些规范,因此希望我能帮助您。这个问题与谈话非常相似9 月份在 JavaOne 上进行捐赠。

1) 我应该使用什么数据类型作为
Web 服务的参数?如何
利用我的架构来实现此用途?我
正在考虑各种 XML 绑定
框架(尤其是 JAXB 和 SDO)
为此但不知道如何
继续。

这取决于您使用的 Web 服务框架。 JAXB 与 JAX-WS 一起使用要容易得多,虽然 JAXB 与 JAX-RS SDO 一起使用仍然更容易,但它是一种可能的替代方案。

2) 如何增强这两项服务
(称之为存储和搜索)使用
动态创建的模板基于
原始的通用模式?这
服务仍会接受文件
主要模式类型,但具有
基于a的内部属性列表
模板说 template1 只需要
其值为整数,而 template2
需要(浮点数)和(字符串)。这
当前基于 JSP 的手动原型
创建此模板,但作为 XML
手工组装的文件
(<>标签分散在文本中)
没有类型检查所以我
我认为我可以做得更好!

我不是 100% 你的意思,但以下内容可能会有所帮助:

3) 是否可以快速生成
用于简单访问的网络应用程序原型
这个系统(再次使用模式
(和模板)编辑适当的
XML 消息结构?我是什么
寻找的是(人类)用户
选择一个模板,然后“填写”
在空白处”并提交,无需
任何奇特的外观和感觉。

JAX-RS 是一个用于创建快速原型的优秀框架。下面是我创建的示例:

4) 我可以或者我怎样才能使用这个
用于通信的 XML 消息类型
跨套接字?

我更喜欢像 JAX-RS 这样通过 HTTP 协议进行通信的框架。

5) 如果我部署
服务是否作为无状态 EJB?做
我需要它们是 EJB 或 servlet
就足够了吗?

我的偏好是使用 EJB 会话 bean 来提供服务。如果您正在与数据库交互,那么您可以利用 Java Transaction API (JTA) 来管理数据库事务。

SDO

EclipseLink 是 SDO 2.1.1 (JSR-235) 参考实现。我们在下面发布了一些示例。如果您正在寻找如何做特定的事情,我会尝试发布一个相关的示例。

JAXB

JAXB 是静态的。它也比 SDO 更受欢迎。认识到这一点,我们在 EclipseLink 中实现了动态 JAXB 功能。它为您提供了具有 JAXB 倾向的 SDO 的动态方面。

编辑 #1

由于您正在处理 JAX-WS 并且您的模型几乎完全是动态的,因此我认为您应该完全跳过 JAXB 绑定。在以下链接中,请参阅“关闭数据绑定”部分

这将为我们提供 javax.xml.transform.Source 对象形式的消息正文。我们需要根据动态模板处理 XML。 SDO 在这里将是一个不错的选择。您可以使用 XML 架构不断向 HelperContext 添加新类型。

helperContext.getXSDHelper().define(schema1, null);
helperContext.getXSDHelper().define(schema2, null);

您将能够从 Web 服务中解组源,如下所示:

XMLDocument doc = helperContext.getXMLHelper().load(source, null, null);
DataObject rootDataObject = doc.getRootObject();
String someValue = rootDataObject.getString("attr3/childAttr/anotherChildAttr");

您还可以在调用其他服务时使用 XMLHelper 将对象编组为 XML。

I lead the EclipseLink JAXB & SDO implementations and represent Oracle on those specifications so hopefully I can help you out. This question is very similar to talk I'm giving at JavaOne in September.

1) What data type should I use as the
parameters of the web services? How to
utilize my schema for this usage? I
was considering various XML binding
frameworks (especially JAXB and SDO)
for this but didn't know how to
proceed.

This depend's on what web service framework you are using. JAXB is much easier to use with JAX-WS, and while JAXB is still easier to use with JAX-RS SDO, is a possible alternative.

2) How can I enhance the two services
(call them store and search) to use
dynamically created templates based on
the original generic schema? The
service would still accept documents
of the main schema type but has the
inner attribute list based on a
template say template1 only requires
whose values are ints while template2
require (float) and (string). The
current JSP-based prototype manually
creates this template but as an XML
document that is assembled by hand
(<>tags dispersed in text) and there
is no type checking what so ever so I
thought I could do better!

I'm not 100% what you mean here, but the following may be helpful:

3) Is it possible to generate a quick
web app prototype for simple access to
this system (again by using the schema
(&templates) to edit the appropriate
XML message structures? What I am
looking for is for the (human) user to
choose a template and then just "fill
in the blanks" and submit, no need for
any fancy look and feel.

JAX-RS is a nice framework for creating quick prototypes. Below is an example I created:

4) Can I or how can I also use this
XML message type for communicating
across sockets?

I prefer frameworks like JAX-RS that communicate over the HTTP protocol.

5) Does it matter if I deploy the
services as stateless EJBs or not? Do
I need them to be EJBs or servlets
would be more than enough?

My preference is to use an EJB session bean for the service. If you are interacting with a database then you can leverage the Java Transaction API (JTA) to manage your database transactions.

SDO

EclipseLink is the SDO 2.1.1 (JSR-235) reference implementation. We have some examples posted below. If you are looking how to do something specific, I will try to post a relevant example.

JAXB

JAXB is static. It is also more popular than SDO. Recognizing this in EclipseLink we have implemented a dynamic JAXB feature. It gives you the dynamic aspect of SDO with a JAXB slant.

EDIT #1

Since you are dealing with JAX-WS and your model is almost entirely dynamic, I think you should skip the JAXB binding altogether. In the following link see the section "Switching Off Data Binding"

This will give us the body of the message as a javax.xml.transform.Source object. We will need to process the XML based on the dynamic templates. SDO would be a good choice here. You can constantly add new types to the HelperContext using XML schemas.

helperContext.getXSDHelper().define(schema1, null);
helperContext.getXSDHelper().define(schema2, null);

You wil be able to unmarshal the Source from the web service as follows:

XMLDocument doc = helperContext.getXMLHelper().load(source, null, null);
DataObject rootDataObject = doc.getRootObject();
String someValue = rootDataObject.getString("attr3/childAttr/anotherChildAttr");

You will also be able to use the XMLHelper to marshal your objects to XML when calling another service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文