Search in sources :

Example 1 with RpcRequest

use of org.opennms.core.rpc.api.RpcRequest in project opennms by OpenNMS.

the class CamelRpcClientPreProcessor method process.

@Override
public void process(Exchange exchange) {
    @SuppressWarnings("unchecked") final CamelRpcRequest<RpcRequest, RpcResponse> wrapper = exchange.getIn().getBody(CamelRpcRequest.class);
    final JmsQueueNameFactory queueNameFactory = new JmsQueueNameFactory(CamelRpcConstants.JMS_QUEUE_PREFIX, wrapper.getModule().getId(), wrapper.getRequest().getLocation());
    exchange.getIn().setHeader(CamelRpcConstants.JMS_QUEUE_NAME_HEADER, queueNameFactory.getName());
    exchange.getIn().setHeader(CamelRpcConstants.CAMEL_JMS_REQUEST_TIMEOUT_HEADER, wrapper.getRequest().getTimeToLiveMs() != null ? wrapper.getRequest().getTimeToLiveMs() : CAMEL_JMS_REQUEST_TIMEOUT);
    final String request = wrapper.getModule().marshalRequest((RpcRequest) wrapper.getRequest());
    exchange.getIn().setBody(request);
}
Also used : RpcRequest(org.opennms.core.rpc.api.RpcRequest) RpcResponse(org.opennms.core.rpc.api.RpcResponse) JmsQueueNameFactory(org.opennms.core.camel.JmsQueueNameFactory)

Example 2 with RpcRequest

use of org.opennms.core.rpc.api.RpcRequest in project opennms by OpenNMS.

the class CamelRpcServerRouteManager method bind.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void bind(RpcModule module) throws Exception {
    if (module != null) {
        final RpcModule<RpcRequest, RpcResponse> rpcModule = (RpcModule<RpcRequest, RpcResponse>) module;
        final String routeId = getRouteId(rpcModule);
        final Route existingRoute = context.getRoute(routeId);
        if (routeIdsByModule.containsKey(rpcModule)) {
            if (existingRoute == null) {
                LOG.error("RpcModule {} ({}) was marked as registered but its route {} cannot be found in the Camel context", rpcModule.getId(), Integer.toHexString(rpcModule.hashCode()), routeId);
            } else {
                LOG.warn("RpcModule {} ({}) was already registered on route {}: {}", rpcModule.getId(), Integer.toHexString(rpcModule.hashCode()), routeId, existingRoute);
            }
        } else {
            if (existingRoute == null) {
                final DynamicRpcRouteBuilder routeBuilder = new DynamicRpcRouteBuilder(context, identity, rpcModule);
                context.addRoutes(routeBuilder);
                routeIdsByModule.put(rpcModule, routeId);
                LOG.info("Registered RpcModule {} ({}) on route {} with queue {}", rpcModule.getId(), Integer.toHexString(rpcModule.hashCode()), routeId, routeBuilder.getQueueName());
            } else {
                LOG.warn("RpcModule {} ({}) cannot be registered, route {} is already present: {}", rpcModule.getId(), Integer.toHexString(rpcModule.hashCode()), routeId, existingRoute);
            }
        }
    }
}
Also used : RpcRequest(org.opennms.core.rpc.api.RpcRequest) RpcModule(org.opennms.core.rpc.api.RpcModule) RpcResponse(org.opennms.core.rpc.api.RpcResponse) Route(org.apache.camel.Route)

Example 3 with RpcRequest

use of org.opennms.core.rpc.api.RpcRequest in project opennms by OpenNMS.

the class CamelRpcServerProcessor method process.

@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    final RpcRequest request = module.unmarshalRequest(exchange.getIn().getBody(String.class));
    final CompletableFuture<RpcResponse> future = module.execute(request);
    future.whenComplete((res, ex) -> {
        try {
            final RpcResponse response;
            if (ex != null) {
                // An exception occurred, store the exception in a new response
                LOG.warn("An error occured while executing a call in {}.", module.getId(), ex);
                response = module.createResponseWithException(ex);
            } else {
                // No exception occurred, use the given response
                response = res;
            }
            try {
                exchange.getOut().setBody(module.marshalResponse(response), String.class);
            } catch (Throwable t) {
                LOG.error("Marshalling a response in RPC module {} failed.", module, t);
                exchange.setException(t);
                exchange.getOut().setFault(true);
            }
        } finally {
            callback.done(false);
        }
    });
    return false;
}
Also used : RpcRequest(org.opennms.core.rpc.api.RpcRequest) RpcResponse(org.opennms.core.rpc.api.RpcResponse)

Aggregations

RpcRequest (org.opennms.core.rpc.api.RpcRequest)3 RpcResponse (org.opennms.core.rpc.api.RpcResponse)3 Route (org.apache.camel.Route)1 JmsQueueNameFactory (org.opennms.core.camel.JmsQueueNameFactory)1 RpcModule (org.opennms.core.rpc.api.RpcModule)1