@Override public AggregationPolicy<TelemetryMessage, TelemetryProtos.TelemetryMessageLog, TelemetryProtos.TelemetryMessageLog.Builder> getAggregationPolicy() { final String systemId = distPollerDao.whoami().getId(); final String systemLocation = distPollerDao.whoami().getLocation(); return new AggregationPolicy<TelemetryMessage, TelemetryProtos.TelemetryMessageLog, TelemetryProtos.TelemetryMessageLog.Builder>() { @Override
/** * Used for testing. */ public SyslogMessageLogDTO toMessageLog(SyslogConnection... connections) { final String systemId = distPollerDao.whoami().getId(); final String systemLocation = distPollerDao.whoami().getLocation(); if (connections.length < 1) { throw new IllegalArgumentException("One or more connection are required."); } final SyslogMessageLogDTO messageLog = new SyslogMessageLogDTO(systemLocation, systemId, connections[0].getSource()); for (SyslogConnection connection : connections) { final SyslogMessageDTO messageDTO = new SyslogMessageDTO(connection.getBuffer()); messageLog.getMessages().add(messageDTO); } return messageLog; }
@Override public AggregationPolicy<SyslogConnection, SyslogMessageLogDTO, SyslogMessageLogDTO> getAggregationPolicy() { final String systemId = distPollerDao.whoami().getId(); final String systemLocation = distPollerDao.whoami().getLocation(); return new AggregationPolicy<SyslogConnection, SyslogMessageLogDTO, SyslogMessageLogDTO>() { @Override
@Override public TrapLogDTO aggregate(TrapLogDTO accumulator, TrapInformationWrapper newMessage) { final TrapInformation trapInfo = newMessage.getTrapInformation(); TrapDTO trapDTO; InetAddress trapAddress; if(trapInfo != null) { trapDTO = transformTrapInfo(trapInfo); trapAddress = TrapUtils.getEffectiveTrapAddress(trapInfo, config.shouldUseAddressFromVarbind()); } else { trapDTO = newMessage.getTrapDTO(); trapAddress = newMessage.getTrapAddress(); } if (accumulator == null) { // no log created yet accumulator = new TrapLogDTO(distPoller.getId(), distPoller.getLocation(), trapAddress); } accumulator.addMessage(trapDTO); return accumulator; }
@Test(timeout=60000) public void canProcessManyRequestsAsynchronously() throws Exception { // Execute a request via a remote location assertNotEquals(REMOTE_LOCATION_NAME, identity.getLocation()); // Lock the run method in our RPC module, we want to validate // the number of threads that are "running" the module CompletableFuture<Integer> runLockedFuture = lockingRpcModule.getRunLocker().waitForThreads(NTHREADS); // Fire off NTHREADS request ThreadLockingEchoClient client = new ThreadLockingEchoClient(rpcClientFactory, lockingRpcModule); List<CompletableFuture<EchoResponse>> futures = new ArrayList<>(); for (int i = 0; i < NTHREADS; i++) { EchoRequest request = new EchoRequest("ping"); request.setTimeToLiveMs(30000L); request.setLocation(REMOTE_LOCATION_NAME); futures.add(client.execute(request)); } // Wait for all the threads calling run() to be locked runLockedFuture.get(); // Release and verify that all the futures return lockingRpcModule.getRunLocker().release(); CompletableFuture.allOf(futures.toArray(new CompletableFuture<?>[NTHREADS])).get(); }
@Override public TrapLogDTO aggregate(TrapLogDTO accumulator, TrapInformationWrapper newMessage) { final TrapInformation trapInfo = newMessage.getTrapInformation(); TrapDTO trapDTO; InetAddress trapAddress; if(trapInfo != null) { trapDTO = transformTrapInfo(trapInfo); trapAddress = TrapUtils.getEffectiveTrapAddress(trapInfo, config.shouldUseAddressFromVarbind()); } else { trapDTO = newMessage.getTrapDTO(); trapAddress = newMessage.getTrapAddress(); } if (accumulator == null) { // no log created yet accumulator = new TrapLogDTO(distPoller.getId(), distPoller.getLocation(), trapAddress); } accumulator.addMessage(trapDTO); return accumulator; }
@Test(timeout=60000) public void canExecuteRpcViaRemoteLocation() throws Exception { // Execute a request via a remote location assertNotEquals(REMOTE_LOCATION_NAME, identity.getLocation()); EchoRequest request = new EchoRequest("HELLO!!!"); request.setLocation(REMOTE_LOCATION_NAME); EchoResponse expectedResponse = new EchoResponse("HELLO!!!"); EchoResponse actualResponse = echoClient.execute(request).get(); assertEquals(expectedResponse, actualResponse); } }
/** * Verifies that the future fails with a {@code RequestRejectedException} when * when the client context is stopped. */ @Test(timeout=60000) public void futureFailsWithRequestRejectedExceptionWhenClientContextIsStopped() throws Exception { assertNotEquals(REMOTE_LOCATION_NAME, identity.getLocation()); // Stop the client context, this will happen when OpenNMS is shutting down getClientContext().stop(); // Now issue an RPC EchoRequest request = new EchoRequest("Helló"); request.setLocation(REMOTE_LOCATION_NAME); try { echoClient.execute(request).get(); fail(); } catch (ExecutionException e) { assertEquals(RequestRejectedException.class, e.getCause().getClass()); } }
/** * Verifies that the future fails with a {@code RemoteExecutionException} when * if an error occurs when executing remotely. */ @Test(timeout=60000) public void futureFailsWithRemoteExecutionExceptionWhenExecutingRemotely() throws Exception { assertNotEquals(REMOTE_LOCATION_NAME, identity.getLocation()); EchoRpcModule echoRpcModule = new EchoRpcModule(); CamelContext context = getContext(); context.start(); CamelRpcServerRouteManager routeManager = getRouteManager(context); routeManager.bind(echoRpcModule); EchoRequest request = new EchoRequest("Oops!"); request.shouldThrow(true); request.setLocation(REMOTE_LOCATION_NAME); try { echoClient.execute(request).get(); fail(); } catch (ExecutionException e) { assertTrue(e.getCause().getMessage(), e.getCause().getMessage().contains("Oops!")); assertEquals(RemoteExecutionException.class, e.getCause().getClass()); } routeManager.unbind(echoRpcModule); context.stop(); }
@Test(timeout=60000) public void canExecuteRpcViaAnotherLocation() throws Exception { assertNotEquals(REMOTE_LOCATION_NAME, identity.getLocation()); EchoRpcModule echoRpcModule = new EchoRpcModule(); CamelContext context = getContext(); context.start(); CamelRpcServerRouteManager routeManager = getRouteManager(context); routeManager.bind(echoRpcModule); EchoRequest request = new EchoRequest("HELLO!!!"); request.setLocation(REMOTE_LOCATION_NAME); EchoResponse expectedResponse = new EchoResponse("HELLO!!!"); EchoResponse actualResponse = echoClient.execute(request).get(); assertEquals(expectedResponse, actualResponse); routeManager.unbind(echoRpcModule); context.stop(); }
/** * Issues a RPC to a location at which a listener is registered, * but specifies a system id that is not equal to the listener's. * Since no matching system can process the request, the request * should time out. */ @Test(timeout=60000) public void failsWithTimeoutWhenSystemIdDoesNotExist() throws Exception { assertNotEquals(REMOTE_LOCATION_NAME, identity.getLocation()); EchoRpcModule echoRpcModule = new EchoRpcModule(); CamelContext context = getContext(); context.start(); MinionIdentity minionIdentity = new MockMinionIdentity(REMOTE_LOCATION_NAME); CamelRpcServerRouteManager routeManager = getRouteManager(context); routeManager.bind(echoRpcModule); EchoRequest request = new EchoRequest("HELLO!!!"); // Use a different system id, other than the one that's actually listening request.setSystemId(minionIdentity.getId() + "!"); request.setLocation(REMOTE_LOCATION_NAME); try { echoClient.execute(request).get(); fail("Did not get ExecutionException"); } catch (ExecutionException e) { assertTrue("Cause is not of type RequestTimedOutException: " + ExceptionUtils.getStackTrace(e), e.getCause() instanceof RequestTimedOutException); } routeManager.unbind(echoRpcModule); context.stop(); }
@Test(timeout=CamelRpcClientPreProcessor.CAMEL_JMS_REQUEST_TIMEOUT_DEFAULT * 4) public void throwsRequestTimedOutExceptionOnTimeout() throws Exception { assertNotEquals(REMOTE_LOCATION_NAME, identity.getLocation()); EchoRpcModule echoRpcModule = new EchoRpcModule();
@Test(timeout=60000) public void canExecuteRpcViaAnotherLocationWithSystemId() throws Exception { assertNotEquals(REMOTE_LOCATION_NAME, identity.getLocation()); EchoRpcModule echoRpcModule = new EchoRpcModule(); CamelContext context = getContext(); context.start(); MinionIdentity minionIdentity = new MockMinionIdentity(REMOTE_LOCATION_NAME); CamelRpcServerRouteManager routeManager = getRouteManager(context); routeManager.bind(echoRpcModule); EchoRequest request = new EchoRequest("HELLO!!!"); // Specify the system id assertNotNull(minionIdentity.getId()); request.setSystemId(minionIdentity.getId()); request.setLocation(REMOTE_LOCATION_NAME); EchoResponse expectedResponse = new EchoResponse("HELLO!!!"); EchoResponse actualResponse = echoClient.execute(request).get(); assertEquals(expectedResponse, actualResponse); routeManager.unbind(echoRpcModule); context.stop(); }