异步Java RMI

发布于 2024-08-14 22:15:46 字数 198 浏览 7 评论 0原文

最近我需要使用 RMI,并且我对我需要做什么已经足够了解,但是当我重新审视这个主题时,有一件事引起了我的兴趣。是否可以对服务器上的同一服务进行异步 RMI 调用?

假设我在客户端有 n 个线程和一个服务器端对象——称之为 S。S 有一个我想从客户端线程调用的方法,但我不希望它被阻塞无需担心共享资源。

有什么想法吗?或者这是最好留给其他方法的东西吗?

Recently I have needed to use RMI, and I know enough for what I need to do, but one thing is intriguing me as I revisit this topic. Is it possible to make asynchronous RMI calls to the same service on the server?

Let's say I have n threads on the client and a single server-side object--call it S. S has a single method that I want to call from my client-side threads, but I don't want it to block as it has no shared resource to worry about.

Any ideas? Or is this something that is just better left to other methods?

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

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

发布评论

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

评论(3

梦归所梦 2024-08-21 22:15:46

这样做应该没有问题。从服务的角度来看,这相当于多个客户端同时调用同一个服务。

任何服务器端对象都应该编写为对于并发访问是安全的。

There should be no issues with doing this. It is the equivalent of multiple clients calling the same service at the same time from the service's perspective.

Any server side object should be written to be safe for concurrent access.

浮生未歇 2024-08-21 22:15:46

也许您应该使用 JMS 队列之类的东西来在 J2EE 架构上进行异步调用。在这些情况下它可以完美地工作。

Probably you should work with something like JMS queue for asynchronic calls on an J2EE architecture. It works perfectly in these cases.

捶死心动 2024-08-21 22:15:46

使用 Redisson 框架远程服务可以在与客户端 Redisson 实例相同的节点上注册,甚至可以在与客户端 Redisson 实例共享的同一 JVM 上注册。

假设 YourServiceImpl 包含您需要远程调用的方法并实现 YourService 接口。

YourServiceImpl 应通过 RemoteService 对象在 Redisson 中注册:

YourService yourService = new YourServiceImpl();

RRemoteService remoteService = redisson.getRemoteService();
remoteService.register(YourService.class, yourService);

远程调用可以以异步方式进行,并标记单独的接口
带有@RRemoteAsync注释。方法签名应与远程接口中的相同方法匹配。
每个方法都应该返回 org.redisson.core.RFuture 对象。它扩展了java.util.concurrent.Future
和 java.util.concurrent.CompletionStage 接口以及
有用的方法很少。

public interface YourService {

    Long someMethod1(Long param1, String param2);

    void someMethod2(MyObject param);

    MyObject someMethod3();

}

// async interface for YourService
@RRemoteAsync(YourService.class)
public interface YourServiceAsync {

    RFuture<Long> someMethod1(Long param1, String param2);

    RFuture<Void> someMethod2(MyObject param);

}

要远程调用方法,请使用 YourServiceAsync 接口:

RRemoteService remoteService = redisson.getRemoteService();
YourServiceAsync asyncService = remoteService.get(YourServiceAsync.class);

RFuture<Long> res = asyncService.someMethod1(12L, "param");
res.thenApply(r -> {
    ...
});

更多文档为 此处

Using Redisson framework remote service could be registered on same node with client side Redisson instance and even on same JVM shared with client side Redisson instance.

Let's assume YourServiceImpl contains method you need to invoke remotely and implements YourService interface.

YourServiceImpl should be registered in Redisson via RemoteService object:

YourService yourService = new YourServiceImpl();

RRemoteService remoteService = redisson.getRemoteService();
remoteService.register(YourService.class, yourService);

Remote invocations could be made in asynchronous manner with separate interface marked
with @RRemoteAsync annotation. Method signatures should be match with same methods in remote interface.
Each method should return org.redisson.core.RFuture object. It extends java.util.concurrent.Future
and java.util.concurrent.CompletionStage interfaces and
has few useful methods.

public interface YourService {

    Long someMethod1(Long param1, String param2);

    void someMethod2(MyObject param);

    MyObject someMethod3();

}

// async interface for YourService
@RRemoteAsync(YourService.class)
public interface YourServiceAsync {

    RFuture<Long> someMethod1(Long param1, String param2);

    RFuture<Void> someMethod2(MyObject param);

}

To invoke method remotely use YourServiceAsync interface:

RRemoteService remoteService = redisson.getRemoteService();
YourServiceAsync asyncService = remoteService.get(YourServiceAsync.class);

RFuture<Long> res = asyncService.someMethod1(12L, "param");
res.thenApply(r -> {
    ...
});

More documentation is here

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