简体   繁体   English

代理在执行相同代码的同时多次调用

[英]Agent called multiple times at the same time executing the same code

I have a Java agents which generates documents. 我有一个生成文档的Java代理。 I also give these document a custom unique follow number, starting at 1 and increment +1 every time. 我还为这些文档提供了一个自定义的唯一跟随号码,从1开始,每次递增+1。 I have a view where I get the last number, I take this number and increment it with one. 我有一个视图,我得到最后一个数字,我取这个数字并用一个增加它。 So whenever the agent runs it increments this number, this happens by a piece of code which is at the bottom of this question. 因此,每当代理运行时,它会递增此数字,这通过一段代码发生,该代码位于此问题的底部。 Sometimes this agents is called simultaneously and this results in getting the same number. 有时会同时调用此代理,这会导致获得相同的数字。 So I have documents for example which has the numer 1001, 4 times and not 1001, 1002,1003,1004. 所以我有文件,例如有数字1001,4次而不是1001,1002,1003,1004。

I tried to look if an agent can run one at a time but this is only the case for scheduled agents. 我试图查看代理是否可以一次运行一个代理,但这只是预定代理的情况。

The code which I run to generate the unique number is: 我运行以生成唯一编号的代码是:

String ReturnValue = "";

                View nvwVolgnr =  iOrderDB.getView("Volgnummer");
                lotus.domino.Document docVolgnr = nvwVolgnr.getDocumentByKey("Order");
                if ( docVolgnr!=null){
                    String strVolgnr = docVolgnr.getItemValue("Volgnummer").toString();

                     //System.out.println("strVolgnr " + strVolgnr);

                    //Object intVolgnr = docVolgnr.getItemValue("Volgnummer");

                    strVolgnr = strVolgnr.replace("[", "");
                    strVolgnr = strVolgnr.replace("]", "");

                    double intVolgnr = Double.parseDouble(strVolgnr);

                    strVolgnr = strVolgnr.replace(".0", "");

                    //System.out.println("strVolgnr " + strVolgnr);

                    strVolgnr = "000000" + strVolgnr;
                    //System.out.println("strVolgnr " + strVolgnr);

                    strVolgnr = strVolgnr.substring(strVolgnr.length() - 6);
                    ReturnValue = strVolgnr;

                    intVolgnr = intVolgnr + 1;
                    Double dblVolgnr = new Double(intVolgnr);

                    //System.out.println("strVolgnr " + strVolgnr);

                    //Object objVolgnr =  intVolgnr;

                    docVolgnr.replaceItemValue("Volgnummer", dblVolgnr);
                     if (docVolgnr.save())
                        {

                        }

Is there any way to get unique numbers (with the increment) even when this agent runs simultaneous 有没有办法获得唯一的数字(增量),即使这个代理同时运行

The easiest way would probably be to set up a single point of truth: This could be an extra local agent, a file, a database or an API. 最简单的方法可能是建立一个单一的事实:这可能是一个额外的本地代理,一个文件,一个数据库或一个API。 You can get the next number from any of these. 您可以从其中任何一个获得下一个号码。

This means that you setup one of these to give your agents the next number. 这意味着您可以设置其中一个,以便为您的代理提供下一个号码。 Since this happens on a single location, you will no longer have any duplicates. 由于这发生在一个位置,您将不再有任何重复。

If incrementing the numbers is not needed, but you just need to have a unique number for each, you can look at UUID's. 如果不需要递增数字,但您只需要为每个数字添加一个唯一的数字,您可以查看UUID。 But I believe that is not the case. 但我相信情况并非如此。

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

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