通过 http 连接到远程数据库的富客户端 (swing) 应用程序

发布于 2024-11-08 20:16:38 字数 341 浏览 0 评论 0原文

我有一个本地客户端 j2se 应用程序,后端是 derby(javadb) 数据库,dao 是 jpa eclipselink 。 我如何将这些数据库pojo发送到与tomcat服务器上的spring(jsp)应用程序链接的远程数据库,

简单地说,这是一个带有swing的富客户端,它连接到tomcat部署的Web应用程序。客户端应该接收数据并通过HTTP请求发送数据到服务的服务器端, 最好的解决方案是什么?

01) 使用 Eclipselink 通过套接字直接数据库连接/事务 02) 网络服务 ?? 03)只需将post请求发送到spring web应用程序并将其转换为POJO并保存到数据库

我如何实现这一点?

i have a local client j2se application and backend is derby(javadb) database and dao is jpa eclipselink .
how do i send these database pojo to a remote database which linked with spring ( jsp) application on tomcat server

simply this is a rich client with swing which connects to tomcat deployed web application. The client should receive data and send data through HTTP requests to the server-side of the service,
what would be the best solution ??

01) direct database connection/transaction through socket using Eclipselink
02) web service ??
03) just send post request to spring web application and convert it to POJO and persist to database

how do i achieve this??

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

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

发布评论

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

评论(1

哀由 2024-11-15 20:16:38

免责声明建议您将应用程序从 Spring 移植到 EJB。尽管人们喜欢将它们仅作为其中之一进行比较,但您可以同时使用它们。这是您的应用程序,您可以随心所欲地务实:)

如果您愿意,您不一定必须使用 Web 服务。您也可以将 OpenEJB war 文件放入 Tomcat 中,并创建一个远程 EJB 来来回发送数据。

一旦你放入 OpenEJB,你就可以在你的应用程序中放置一个远程 @Stateless bean,如下所示:

@Stateless
@Remote
public class MyBean implements MyBeanRemote {
    //...
}

public interface MyBeanRemote {
   // any methods you want remotely invoked
}

然后从你的 Swing 应用程序中查找它并通过 HTTP 执行它,如下所示:

Properties p = new Properties();
p.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
p.put("java.naming.provider.url", "http://tomcatserver:8080/openejb/ejb");
// user and pass optional
p.put("java.naming.security.principal", "myuser");
p.put("java.naming.security.credentials", "mypass");

InitialContext ctx = new InitialContext(p);

MyBean myBean = (MyBean) ctx.lookup("MyBeanRemote");

客户端你需要的只是 openejb-client.jar 和 javaee来自 OpenEJB war 文件和您自己的类的 -api.jar。

由于它已经是一个 Spring 应用程序,因此不必费心尝试使用 @PersistenceContext 来获取对 EntityManager 的引用,以便 EJB 可以使用它。只需弄清楚如何通过任何可能的方式将 Spring 创建(或您创建)的 EntityManagerFactory 公开给 EJB。直接且丑陋但有效的方法是 MyBean 类的静态以及设置它的一些启动逻辑。您只需使用 EJB 进行远程处理,因此不需要更高级的集成。

如果您确实需要 Web 服务进行非 Java 通信或其他内容,您可以将 @WebService 添加到您的 bean 顶部,然后它将具有 WSDL 以及为其生成的所有内容:

@Stateless
@Remote
@WebService(portName = "MyBeanPort",
    serviceName = "MyBeanService",
    targetNamespace = "http://superbiz.org/wsdl"
    endpointInterface = "org.superbiz.MyBeanRemote")
public class MyBean implements MyBeanRemote {
    //...
}

public interface MyBeanRemote {
   // any methods you want remotely invoked
}

然后您还可以使用与 Web 服务相同的 bean,例如:

Service service = Service.create(
        new URL("http://tomcatserver:8080/MyBeanImpl?wsdl"),
        new QName("http://superbiz.org/wsdl", "MyBeanService"));
assertNotNull(service);

MyBeanRemote myBean = service.getPort(MyBeanRemote.class);

两种方法都通过 http,但 Web 服务方法会慢一些,因为它不是二进制协议。

DISCLAIMER I am not suggesting you port your app from Spring to EJB. Despite how people like to compare them as exclusively one or the other, you can use them both. Its your app, you can be as pragmatic as you want to be :)

You don't necessarily have to use Web Services if you wanted. You could drop the OpenEJB war file into Tomcat as well and create an Remote EJB to send data back and forth.

Once you drop in OpenEJB you can put a remote @Stateless bean in your app like so:

@Stateless
@Remote
public class MyBean implements MyBeanRemote {
    //...
}

public interface MyBeanRemote {
   // any methods you want remotely invoked
}

Then look it up and execute it over HTTP from your Swing app like so:

Properties p = new Properties();
p.put("java.naming.factory.initial", "org.apache.openejb.client.RemoteInitialContextFactory");
p.put("java.naming.provider.url", "http://tomcatserver:8080/openejb/ejb");
// user and pass optional
p.put("java.naming.security.principal", "myuser");
p.put("java.naming.security.credentials", "mypass");

InitialContext ctx = new InitialContext(p);

MyBean myBean = (MyBean) ctx.lookup("MyBeanRemote");

Client-side all you need are the openejb-client.jar and javaee-api.jar from the OpenEJB war file and your own classes.

Since it's already a Spring app don't bother trying to use @PersistenceContext to get a reference to the EntityManager so the EJB can use it. Just figure out how to expose the EntityManagerFactory that Spring creates (or you create) to the EJB via any means possible. The direct and ugly, but effective, approach would be a static on the MyBean class and a bit of startup logic that sets it. You'd just be using the EJB for remoting so no need for fancier integration.

If you did really need web services for non-java communication or something, you can add @WebService to the top of your bean and then it will have WSDL and all that generated for it:

@Stateless
@Remote
@WebService(portName = "MyBeanPort",
    serviceName = "MyBeanService",
    targetNamespace = "http://superbiz.org/wsdl"
    endpointInterface = "org.superbiz.MyBeanRemote")
public class MyBean implements MyBeanRemote {
    //...
}

public interface MyBeanRemote {
   // any methods you want remotely invoked
}

Then you can also use the same bean as a web service like:

Service service = Service.create(
        new URL("http://tomcatserver:8080/MyBeanImpl?wsdl"),
        new QName("http://superbiz.org/wsdl", "MyBeanService"));
assertNotNull(service);

MyBeanRemote myBean = service.getPort(MyBeanRemote.class);

Both approaches are over http, but the web service approach will be a bit slower as it isn't a binary protocol.

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