Search in sources :

Example 16 with RequestOptions

use of org.jgroups.blocks.RequestOptions in project JGroups by belaban.

the class QuoteClient method actionPerformed.

public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    try {
        switch(command) {
            case "Get":
                {
                    String stock_name = stock_field.getText();
                    if (stock_name == null || stock_name.isEmpty()) {
                        showMsg("Stock name is empty !");
                        return;
                    }
                    showMsg("Looking up value for " + stock_name + ':');
                    RspList<Object> quotes = disp.callRemoteMethods(null, "getQuote", new Object[] { stock_name }, new Class[] { String.class }, new RequestOptions(ResponseMode.GET_ALL, 10000));
                    Float val = null;
                    for (Rsp<Object> rsp : quotes.values()) {
                        Object quote = rsp.getValue();
                        if (quote == null || quote instanceof Throwable)
                            continue;
                        val = (Float) quote;
                        break;
                    }
                    if (val != null) {
                        value_field.setText(val.toString());
                        clearMsg();
                    } else {
                        value_field.setText("");
                        showMsg("Value for " + stock_name + " not found");
                    }
                    break;
                }
            case "Set":
                String stock_name = stock_field.getText();
                String stock_val = value_field.getText();
                if (stock_name == null || stock_val == null || stock_name.isEmpty() || stock_val.isEmpty()) {
                    showMsg("Stock name and value have to be present to enter a new value");
                    return;
                }
                Float val = new Float(stock_val);
                disp.callRemoteMethods(null, "setQuote", new Object[] { stock_name, val }, new Class[] { String.class, Float.class }, new RequestOptions(ResponseMode.GET_FIRST, 0));
                showMsg("Stock " + stock_name + " set to " + val);
                break;
            case "All":
                listbox.removeAll();
                showMsg("Getting all stocks:");
                RspList<Object> rsp_list = disp.callRemoteMethods(null, "getAllStocks", null, null, new RequestOptions(ResponseMode.GET_ALL, 5000));
                System.out.println("rsp_list is " + rsp_list);
                Map<String, Float> all_stocks = null;
                for (Rsp rsp : rsp_list.values()) {
                    Object obj = rsp.getValue();
                    if (obj == null || obj instanceof Throwable)
                        continue;
                    all_stocks = (Map<String, Float>) obj;
                    break;
                }
                if (all_stocks == null) {
                    showMsg("No stocks found");
                    return;
                }
                clearMsg();
                listbox.removeAll();
                all_stocks.entrySet().stream().filter(entry -> entry.getValue() != null).forEach(entry -> listbox.add(entry.getKey() + ": " + entry.getValue().toString()));
                break;
            case "Quit":
                setVisible(false);
                channel.close();
                System.exit(0);
            default:
                System.out.println("Unknown action");
                break;
        }
    } catch (Exception ex) {
        value_field.setText("");
        ex.printStackTrace();
        showMsg(ex.toString());
    }
}
Also used : java.awt(java.awt) Rsp(org.jgroups.util.Rsp) ActionListener(java.awt.event.ActionListener) RequestOptions(org.jgroups.blocks.RequestOptions) ResponseMode(org.jgroups.blocks.ResponseMode) RpcDispatcher(org.jgroups.blocks.RpcDispatcher) WindowListener(java.awt.event.WindowListener) Map(java.util.Map) org.jgroups(org.jgroups) RspList(org.jgroups.util.RspList) ActionEvent(java.awt.event.ActionEvent) WindowEvent(java.awt.event.WindowEvent) RequestOptions(org.jgroups.blocks.RequestOptions) RspList(org.jgroups.util.RspList) Rsp(org.jgroups.util.Rsp)

Example 17 with RequestOptions

use of org.jgroups.blocks.RequestOptions in project JGroups by belaban.

the class GraphPanel method mouseReleased.

public void mouseReleased(MouseEvent e) {
    Point p = e.getPoint();
    if (pick == null)
        return;
    pick.x = p.x;
    pick.y = p.y;
    pick.fixed = pickfixed;
    try {
        MethodCall call = new MethodCall("moveNode", new Object[] { pick }, new Class[] { Node.class });
        wb.disp.callRemoteMethods(null, call, new RequestOptions(ResponseMode.GET_ALL, 0));
    } catch (Exception ex) {
        log.error(ex.toString());
    }
    pick = null;
}
Also used : RequestOptions(org.jgroups.blocks.RequestOptions) MethodCall(org.jgroups.blocks.MethodCall)

Example 18 with RequestOptions

use of org.jgroups.blocks.RequestOptions in project teiid by teiid.

the class JGroupsOutputStream method flush.

public void flush() throws IOException {
    checkClosed();
    try {
        if (index == 0) {
            return;
        }
        disp.callRemoteMethods(dests, new MethodCall((short) (methodOffset + 1), new Object[] { stateId, Arrays.copyOf(buffer, index) }), new RequestOptions(ResponseMode.GET_NONE, 0).setAnycasting(dests != null));
        index = 0;
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : RequestOptions(org.jgroups.blocks.RequestOptions) IOException(java.io.IOException) MethodCall(org.jgroups.blocks.MethodCall) IOException(java.io.IOException)

Example 19 with RequestOptions

use of org.jgroups.blocks.RequestOptions in project JGroups by belaban.

the class RelayDemoRpc method start.

public void start(String props, String name) throws Exception {
    ch = new JChannel(props);
    if (name != null)
        ch.setName(name);
    disp = new RpcDispatcher(ch, this).setReceiver(this);
    ch.connect("RelayDemo");
    local_addr = ch.getAddress().toString();
    MethodCall call = new MethodCall(getClass().getMethod("handleMessage", String.class, String.class));
    for (; ; ) {
        String line = Util.readStringFromStdin(": ");
        if (line.startsWith("help")) {
            System.out.println("unicast <text>  // unicasts to all members of local view\n" + "site <site>+    // unicasts to all listed site masters, e.g. \"site sfo lon\"\n" + "mcast <site>+   // anycasts to all local members, plus listed site masters \n" + "<text>          // multicast, RELAY2 will relay to all members of sites");
            continue;
        }
        call.setArgs(line, local_addr);
        // unicast to every member of the local cluster
        if (line.equalsIgnoreCase("unicast")) {
            for (Address dest : view.getMembers()) {
                System.out.println("invoking method in " + dest + ": ");
                try {
                    Object rsp = disp.callRemoteMethod(dest, call, new RequestOptions(ResponseMode.GET_ALL, RPC_TIMEOUT));
                    System.out.println("rsp from " + dest + ": " + rsp);
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
            }
        } else // unicast to 1 SiteMaster
        if (line.startsWith("site")) {
            Collection<String> site_masters = parseSiteMasters(line.substring("site".length()));
            for (String site_master : site_masters) {
                try {
                    SiteMaster dest = new SiteMaster(site_master);
                    System.out.println("invoking method in " + dest + ": ");
                    Object rsp = disp.callRemoteMethod(dest, call, new RequestOptions(ResponseMode.GET_ALL, RPC_TIMEOUT));
                    System.out.println("rsp from " + dest + ": " + rsp);
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        } else // mcast to all local members and N SiteMasters
        if (line.startsWith("mcast")) {
            Collection<String> site_masters = parseSiteMasters(line.substring("mcast".length()));
            Collection<Address> dests = new ArrayList<>(site_masters.size());
            for (String site_master : site_masters) {
                try {
                    dests.add(new SiteMaster(site_master));
                } catch (Throwable t) {
                    System.err.println("failed adding SiteMaster for " + site_master + ": " + t);
                }
            }
            dests.addAll(view.getMembers());
            System.out.println("invoking method in " + dests + ": ");
            RspList<Object> rsps = disp.callRemoteMethods(dests, call, new RequestOptions(ResponseMode.GET_ALL, RPC_TIMEOUT).anycasting(true));
            for (Map.Entry<Address, Rsp<Object>> entry : rsps.entrySet()) {
                Address sender = entry.getKey();
                Rsp<Object> rsp = entry.getValue();
                if (rsp.wasUnreachable())
                    System.out.println("<< unreachable: " + sender);
                else
                    System.out.println("<< " + rsp.getValue() + " from " + sender);
            }
        } else {
            // mcasting the call to all local cluster members
            RspList<Object> rsps = disp.callRemoteMethods(null, call, new RequestOptions(ResponseMode.GET_ALL, RPC_TIMEOUT).anycasting(false));
            rsps.forEach((key, val) -> System.out.println("<< " + val.getValue() + " from " + key));
        }
    }
}
Also used : RpcDispatcher(org.jgroups.blocks.RpcDispatcher) RequestOptions(org.jgroups.blocks.RequestOptions) MethodCall(org.jgroups.blocks.MethodCall) Rsp(org.jgroups.util.Rsp) SiteMaster(org.jgroups.protocols.relay.SiteMaster)

Example 20 with RequestOptions

use of org.jgroups.blocks.RequestOptions in project JGroups by belaban.

the class RSVPTest method testRpcWithFuture.

public void testRpcWithFuture() throws Exception {
    final Method method = getClass().getMethod("getGreeting");
    RpcDispatcher[] dispatchers = new RpcDispatcher[channels.length];
    for (int i = 0; i < dispatchers.length; i++) {
        channels[i].setReceiver(null);
        dispatchers[i] = new RpcDispatcher(channels[i], this);
        dispatchers[i].start();
    }
    DISCARD discard = channels[0].getProtocolStack().findProtocol(DISCARD.class);
    discard.dropDownMulticasts(1);
    RequestOptions opts = RequestOptions.SYNC().flags(Message.Flag.RSVP_NB);
    long start = System.currentTimeMillis();
    Future<RspList<String>> future = dispatchers[0].callRemoteMethodsWithFuture(null, new MethodCall(method), opts);
    long rpc_time = System.currentTimeMillis() - start;
    start = System.currentTimeMillis();
    RspList<String> rsps = future.get(3000, TimeUnit.MILLISECONDS);
    long get_time = System.currentTimeMillis() - start;
    System.out.printf("rsps=\n%s\nRPC time=%d ms, Get time=%d ms", rsps, rpc_time, get_time);
    assert rsps.size() == channels.length;
    for (Rsp rsp : rsps) assert rsp.wasReceived() && rsp.getValue() != null;
    // take a GC into account
    assert rpc_time < 500;
}
Also used : RpcDispatcher(org.jgroups.blocks.RpcDispatcher) RequestOptions(org.jgroups.blocks.RequestOptions) RspList(org.jgroups.util.RspList) BeforeMethod(org.testng.annotations.BeforeMethod) AfterMethod(org.testng.annotations.AfterMethod) Method(java.lang.reflect.Method) MethodCall(org.jgroups.blocks.MethodCall) Rsp(org.jgroups.util.Rsp)

Aggregations

RequestOptions (org.jgroups.blocks.RequestOptions)31 MethodCall (org.jgroups.blocks.MethodCall)15 Rsp (org.jgroups.util.Rsp)10 RpcDispatcher (org.jgroups.blocks.RpcDispatcher)8 IOException (java.io.IOException)7 CancellationException (java.util.concurrent.CancellationException)4 ExecutionException (java.util.concurrent.ExecutionException)4 TimeoutException (java.util.concurrent.TimeoutException)4 BytesMessage (org.jgroups.BytesMessage)4 Message (org.jgroups.Message)4 MessageDispatcher (org.jgroups.blocks.MessageDispatcher)4 ResponseMode (org.jgroups.blocks.ResponseMode)4 RspList (org.jgroups.util.RspList)4 Map (java.util.Map)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 org.jgroups (org.jgroups)3 Address (org.jgroups.Address)3 CommandDispatcherException (org.wildfly.clustering.dispatcher.CommandDispatcherException)3 Method (java.lang.reflect.Method)2 InetAddress (java.net.InetAddress)2