Search in sources :

Example 1 with RemoteCommand

use of org.exoplatform.services.rpc.RemoteCommand in project kernel by exoplatform.

the class AbstractRPCService method handle.

/**
 * {@inheritDoc}
 */
public Object handle(Message msg) {
    String commandId = null;
    try {
        // Ensure that the service is fully started before trying to execute any command
        startSignal.await();
        MessageBody body = (MessageBody) msg.getObject();
        commandId = body.getCommandId();
        if (!body.accept(getLocalAddress())) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("Command : " + commandId + " needs to be executed on the coordinator " + "only and the local node is not the coordinator, the command will be ignored");
            }
            return null;
        }
        RemoteCommand command = getCommand(commandId);
        if (command == null) {
            return new RPCException("Command " + commandId + " unkown, please register your command first");
        }
        Object execResult = command.execute(body.getArgs());
        if (LOG.isTraceEnabled()) {
            LOG.trace("Command : " + commandId + " executed, result is: " + execResult);
        }
        return execResult;
    } catch (// NOSONAR
    Throwable x) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Problems invoking command.", x);
        }
        return new RPCException("Cannot execute the command " + (commandId == null ? "" : commandId), x);
    }
}
Also used : RPCException(org.exoplatform.services.rpc.RPCException) RemoteCommand(org.exoplatform.services.rpc.RemoteCommand)

Example 2 with RemoteCommand

use of org.exoplatform.services.rpc.RemoteCommand in project kernel by exoplatform.

the class TestRPCServiceImpl method testExecOnCoordinator.

public void testExecOnCoordinator() throws Exception {
    InitParams params = new InitParams();
    ValueParam paramConf = new ValueParam();
    paramConf.setName(RPCServiceImpl.PARAM_JGROUPS_CONFIG);
    paramConf.setValue("jar:/conf/portal/udp.xml");
    params.addParameter(paramConf);
    final List<Boolean> calledCommands = Collections.synchronizedList(new ArrayList<Boolean>());
    RPCServiceImpl service1 = null;
    RPCServiceImpl service2 = null;
    try {
        service1 = new RPCServiceImpl(container.getContext(), params, configManager);
        RemoteCommand service1Cmd = new RemoteCommand() {

            public String getId() {
                return "CoordinatorExecutedCommand";
            }

            public String execute(Serializable[] args) throws Throwable {
                calledCommands.add(Boolean.TRUE);
                return "service 1";
            }
        };
        service1.registerCommand(service1Cmd);
        service2 = new RPCServiceImpl(container.getContext(), params, configManager);
        RemoteCommand service2Cmd = new RemoteCommand() {

            public String getId() {
                return "CoordinatorExecutedCommand";
            }

            public String execute(Serializable[] args) throws Throwable {
                calledCommands.add(Boolean.TRUE);
                return "service 2";
            }
        };
        service2.registerCommand(service2Cmd);
        // starting services
        service1.start();
        service2.start();
        Object o = service1.executeCommandOnCoordinator(service1Cmd, true);
        assertEquals("service 1", o);
        // it should be executed once
        assertEquals(1, calledCommands.size());
    } finally {
        if (service1 != null) {
            service1.stop();
        }
        if (service2 != null) {
            service2.stop();
        }
    }
}
Also used : InitParams(org.exoplatform.container.xml.InitParams) RemoteCommand(org.exoplatform.services.rpc.RemoteCommand) ValueParam(org.exoplatform.container.xml.ValueParam) RPCServiceImpl(org.exoplatform.services.rpc.jgv3.RPCServiceImpl)

Example 3 with RemoteCommand

use of org.exoplatform.services.rpc.RemoteCommand in project kernel by exoplatform.

the class AbstractRPCService method registerCommand.

/**
 * {@inheritDoc}
 */
public synchronized RemoteCommand registerCommand(RemoteCommand command) {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkPermission(RPCService.ACCESS_RPC_SERVICE_PERMISSION);
    }
    if (command != null) {
        String commandId = command.getId();
        if (commandId == null) {
            throw new IllegalArgumentException("The command Id cannot be null");
        }
        Map<String, RemoteCommand> tmpCommands = new HashMap<String, RemoteCommand>(this.commands);
        RemoteCommand oldCommand = tmpCommands.put(commandId, command);
        if (oldCommand != null && PropertyManager.isDevelopping()) {
            LOG.warn("A command has already been registered with the id " + commandId + ", this command will be replaced with the new one");
        }
        this.commands = Collections.unmodifiableMap(tmpCommands);
        return command;
    }
    return null;
}
Also used : HashMap(java.util.HashMap) RemoteCommand(org.exoplatform.services.rpc.RemoteCommand)

Example 4 with RemoteCommand

use of org.exoplatform.services.rpc.RemoteCommand in project kernel by exoplatform.

the class TestRPCServiceImpl method testCommands.

public void testCommands() throws Exception {
    InitParams params = new InitParams();
    ValueParam paramConf = new ValueParam();
    paramConf.setName(RPCServiceImpl.PARAM_JGROUPS_CONFIG);
    paramConf.setValue("jar:/conf/portal/udp.xml");
    params.addParameter(paramConf);
    RPCServiceImpl service = null;
    try {
        service = new RPCServiceImpl(container.getContext(), params, configManager);
        RemoteCommand fake = new RemoteCommand() {

            public String getId() {
                return "fake";
            }

            public String execute(Serializable[] args) throws Throwable {
                return null;
            }
        };
        RemoteCommand fake2 = new RemoteCommand() {

            public String getId() {
                return "fake2";
            }

            public String execute(Serializable[] args) throws Throwable {
                return null;
            }
        };
        RemoteCommand fake2_Unregistered = new RemoteCommand() {

            public String getId() {
                return "fake2";
            }

            public String execute(Serializable[] args) throws Throwable {
                return null;
            }
        };
        service.registerCommand(fake2);
        RemoteCommand Exception = new RemoteCommand() {

            public String getId() {
                return "Exception";
            }

            public String execute(Serializable[] args) throws Throwable {
                throw new Exception("MyException");
            }
        };
        service.registerCommand(Exception);
        RemoteCommand Error = new RemoteCommand() {

            public String getId() {
                return "Error";
            }

            public String execute(Serializable[] args) throws Throwable {
                throw new Error("MyError");
            }
        };
        service.registerCommand(Error);
        RemoteCommand StringValue = new RemoteCommand() {

            public String getId() {
                return "StringValue";
            }

            public String execute(Serializable[] args) throws Throwable {
                return "OK";
            }
        };
        service.registerCommand(StringValue);
        RemoteCommand NullValue = new RemoteCommand() {

            public String getId() {
                return "NullValue";
            }

            public String execute(Serializable[] args) throws Throwable {
                return null;
            }
        };
        service.registerCommand(NullValue);
        RemoteCommand LongTask = new RemoteCommand() {

            public String getId() {
                return "LongTask";
            }

            public String execute(Serializable[] args) throws Throwable {
                Thread.sleep(2000);
                return null;
            }
        };
        service.registerCommand(LongTask);
        service.start();
        try {
            service.executeCommandOnAllNodes(fake, true);
            fail("We expect a RPCException since the command is unknown");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.executeCommandOnCoordinator(fake, true);
            fail("We expect a RPCException since the command is unknown");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.executeCommandOnAllNodes(fake2_Unregistered, true);
            fail("We expect a RPCException since the command is unknown");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.executeCommandOnCoordinator(fake2_Unregistered, true);
            fail("We expect a RPCException since the command is unknown");
        } catch (RPCException e) {
        // OK
        }
        List<Object> result;
        result = service.executeCommandOnAllNodes(Exception, true);
        assertTrue(result != null && result.size() == 1);
        assertTrue("We expect a RPCException since one node could not execute the command", result.get(0) instanceof RPCException);
        try {
            service.executeCommandOnCoordinator(Exception, true);
            fail("We expect a RPCException since one node could not execute the command");
        } catch (RPCException e) {
        // OK
        }
        result = service.executeCommandOnAllNodes(Error, true);
        assertTrue(result != null && result.size() == 1);
        assertTrue("We expect a RPCException since one node could not execute the command", result.get(0) instanceof RPCException);
        try {
            service.executeCommandOnCoordinator(Error, true);
            fail("We expect a RPCException since one node could not execute the command");
        } catch (RPCException e) {
        // OK
        }
        result = service.executeCommandOnAllNodes(LongTask, true);
        assertNotNull(result);
        assertTrue(result.size() == 1);
        assertNull(result.get(0));
        Object o = service.executeCommandOnCoordinator(LongTask, true);
        assertNull(o);
        result = service.executeCommandOnAllNodes(LongTask, 1000);
        assertNotNull(result);
        assertTrue(result.size() == 1);
        assertTrue("We expect an RPCException due to a Replication Timeout", result.get(0) instanceof RPCException);
        try {
            service.executeCommandOnCoordinator(LongTask, 1000);
            fail("We expect an RPCException due to a Replication Timeout");
        } catch (RPCException e) {
        // OK
        }
        result = service.executeCommandOnAllNodes(LongTask, false);
        assertNotNull(result);
        assertTrue(result.isEmpty());
        assertNull(service.executeCommandOnCoordinator(LongTask, false));
        result = service.executeCommandOnAllNodes(StringValue, true);
        assertNotNull(result);
        assertTrue(result.size() == 1);
        assertEquals("OK", result.get(0));
        o = service.executeCommandOnCoordinator(StringValue, true);
        assertEquals("OK", o);
        result = service.executeCommandOnAllNodes(NullValue, true);
        assertNotNull(result);
        assertTrue(result.size() == 1);
        assertNull(result.get(0));
        o = service.executeCommandOnCoordinator(NullValue, true);
        assertNull(o);
    } finally {
        if (service != null) {
            service.stop();
        }
    }
}
Also used : InitParams(org.exoplatform.container.xml.InitParams) RPCException(org.exoplatform.services.rpc.RPCException) RemoteCommand(org.exoplatform.services.rpc.RemoteCommand) ValueParam(org.exoplatform.container.xml.ValueParam) RPCServiceImpl(org.exoplatform.services.rpc.jgv3.RPCServiceImpl) RPCException(org.exoplatform.services.rpc.RPCException) MemberHasLeftException(org.exoplatform.services.rpc.impl.AbstractRPCService.MemberHasLeftException)

Example 5 with RemoteCommand

use of org.exoplatform.services.rpc.RemoteCommand in project kernel by exoplatform.

the class TestRPCServiceImpl method testStates.

public void testStates() throws Exception {
    InitParams params = new InitParams();
    ValueParam paramConf = new ValueParam();
    paramConf.setName(RPCServiceImpl.PARAM_JGROUPS_CONFIG);
    paramConf.setValue("jar:/conf/portal/udp.xml");
    params.addParameter(paramConf);
    RPCServiceImpl service = null;
    RemoteCommand foo = new RemoteCommand() {

        public String getId() {
            return "foo";
        }

        public String execute(Serializable[] args) throws Throwable {
            return null;
        }
    };
    try {
        service = new RPCServiceImpl(container.getContext(), params, configManager);
        service.registerCommand(foo);
        try {
            service.executeCommandOnAllNodes(foo, true);
            fail("We expect a RPCException since the current state is not the expected one");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.executeCommandOnAllNodes(foo, 10);
            fail("We expect a RPCException since the current state is not the expected one");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.executeCommandOnCoordinator(foo, true);
            fail("We expect a RPCException since the current state is not the expected one");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.executeCommandOnCoordinator(foo, 10);
            fail("We expect a RPCException since the current state is not the expected one");
        } catch (RPCException e) {
        // OK
        }
        try {
            service.isCoordinator();
            fail("We expect a RPCException since the current state is not the expected one");
        } catch (RPCException e) {
        // OK
        }
        service.start();
        assertEquals(true, service.isCoordinator());
        service.executeCommandOnAllNodes(foo, true);
        service.executeCommandOnAllNodes(foo, 10);
        service.executeCommandOnCoordinator(foo, true);
        service.executeCommandOnCoordinator(foo, 10);
    } finally {
        if (service != null) {
            service.stop();
        }
    }
    try {
        service.executeCommandOnAllNodes(foo, true);
        fail("We expect a RPCException since the current state is not the expected one");
    } catch (RPCException e) {
    // OK
    }
    try {
        service.executeCommandOnAllNodes(foo, 10);
        fail("We expect a RPCException since the current state is not the expected one");
    } catch (RPCException e) {
    // OK
    }
    try {
        service.executeCommandOnCoordinator(foo, true);
        fail("We expect a RPCException since the current state is not the expected one");
    } catch (RPCException e) {
    // OK
    }
    try {
        service.executeCommandOnCoordinator(foo, 10);
        fail("We expect a RPCException since the current state is not the expected one");
    } catch (RPCException e) {
    // OK
    }
}
Also used : InitParams(org.exoplatform.container.xml.InitParams) RPCException(org.exoplatform.services.rpc.RPCException) RemoteCommand(org.exoplatform.services.rpc.RemoteCommand) ValueParam(org.exoplatform.container.xml.ValueParam) RPCServiceImpl(org.exoplatform.services.rpc.jgv3.RPCServiceImpl)

Aggregations

RemoteCommand (org.exoplatform.services.rpc.RemoteCommand)7 InitParams (org.exoplatform.container.xml.InitParams)5 ValueParam (org.exoplatform.container.xml.ValueParam)5 RPCException (org.exoplatform.services.rpc.RPCException)5 RPCServiceImpl (org.exoplatform.services.rpc.jgv3.RPCServiceImpl)5 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 MemberHasLeftException (org.exoplatform.services.rpc.impl.AbstractRPCService.MemberHasLeftException)2 HashMap (java.util.HashMap)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 SingleMethodCallCommand (org.exoplatform.services.rpc.SingleMethodCallCommand)1 Address (org.jgroups.Address)1