简体   繁体   English

如何在Java RMI中使用可序列化对象和回调方法正确模仿传递引用?

[英]How do I properly mimic pass-by-reference in Java RMI with serializable objects and a callback method?

I have a distributed application using java RMI and a main object(CoreApplication) that implements java.io.Serializable. 我有一个使用java RMI的分布式应用程序和一个实现java.io.Serializable的主对象(CoreApplication)。 Every minute, this main object is sent to a remote computer and processed on that JVM's thread pool. 每分钟,此主对象都将发送到远程计算机并在该JVM的线程池上进行处理。 Since it's asynchronous, the object is processed without blocking the main thread on the Master computer. 由于它是异步的,因此处理对象时不会阻塞主计算机上的主线程。

When the CoreApplication object is finished processing on the remote thread, it invokes a call back method and is sent back to the main computer. 当CoreApplication对象在远程线程上完成处理时,它会调用回调方法并将其发送回主计算机。

Here is some code of the remote machine processing a job invoked from the Master computer via RMI and the sendJob method 以下是远程机器的一些代码,用于处理通过RMI和sendJob方法从主计算机调用的作业

public void sendJob(final CoreApplication aJob) throws RemoteException{
    Runnable r = new Runnable(){
        public void run(){
            try {
                WorkResponse wr = aJob.process();
                client.coreApplicationHandler(aJob,wr); 
            }catch(RemoteException e){}
        }
    };
    workQueue.execute(r);
}

You can see client.coreApplicationHandler is the callback method to the main server and sends the CoreApplication object back with it, along with a response object. 您可以看到client.coreApplicationHandler是主服务器的回调方法,并将CoreApplication对象与响应对象一起发回。

Here is the coreApplicationHandler method code on the main machine 这是主机上的coreApplicationHandler方法代码

    public void coreApplicationHandler(CoreApplication j,WorkResponse wr){
        String ticker = j.getTickerSymbol();
        coreApplicationObjects.put(ticker, j);
        if(GlobalParameters.DEBUG_MODE){ 
            System.out.println("WORK RESPONSE IS "+wr.getMessage());
        }
    }

My question is, is replacing the CoreApplication object each time on the call back method the best way to make sure it's up-to-date for the next minute it's sent? 我的问题是,每次在回调方法上更换CoreApplication对象是确保它在下一分钟发送时最新的最佳方法吗? CoreApplication is fluid and changes and the state must be preserved. CoreApplication是流动的并且变化并且必须保持状态。 I am sending it back to the Master computer, so it's state can be monitored from a central location. 我将它发送回主计算机,因此可以从中央位置监控状态。 If I had 100 computation nodes and they didn't return their objects, it would get really messy I think to keep track of them all. 如果我有100个计算节点并且它们没有返回它们的对象,那么我认为它会变得非常混乱。

It works pretty good so far unless the job isn't processed by the time it tries to send out another and results in sending a stale object with an old state (ie, the same object as the last minute). 到目前为止它工作得很好,除非在尝试发送另一个作业时没有处理该作业并导致发送具有旧状态的陈旧对象(即,与最后一分钟相同的对象)。 Please comment if this doesn't make sense and I will do my best to explain it. 如果这没有意义,请评论我会尽力解释。

RMI is not a way to synchronize objects across a cluster. RMI不是跨群集同步对象的方法。 But there are tools to do just that. 但是有一些工具可以做到这一点。 Look at http://www.hazelcast.com/ , for example. 例如, 查看http://www.hazelcast.com/

If you have a cluster of computers and need synchronization then you need to use clustering via the server's tools or a third party tool. 如果您有一组计算机并且需要同步,那么您需要通过服务器工具或第三方工具使用群集。

I recommend hazelcast. 我推荐hazelcast。 It is very easy to use and will allow local clusters using fast UDP or WAN clusters using TCP socket to TCP socket. 它非常易于使用,并允许使用TCP套接字到TCP套接字的快速UDP或WAN群集的本地群集。

For example Hazelcast will let you do something like this: 例如,Hazelcast会让你做这样的事情:

import com.hazelcast.core.MultiMap;
import com.hazelcast.core.Hazelcast;
import java.util.Collection;

// a live shared multimap shared across all cluster nodes
MultiMap<String, Order> mmCustomerOrders = Hazelcast.getMultiMap("customerOrders");

mmCustomerOrders.put("1", new Order ("iPhone", 340));

Thread.Sleep( 1000 );

Order order = (Order) mmCustomerOrders.get("1");

System.out.println( Order.quantity() ); // 340 ?? Nobody knows, it might have been changed

What is the output? 什么是输出? If a cluster member changed the item "1" in the map, then you will get that value automagically. 如果集群成员在地图中更改了项目“1”,那么您将自动获得该值。 No more coding necessary…. 不再需要编码......

Hope it helps 希望能帮助到你

-Alex -Alex

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

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