Search in sources :

Example 1 with YaCyServices

use of net.yacy.grid.YaCyServices in project yacy_grid_mcp by yacy.

the class PeekService method serviceImpl.

@Override
public ServiceResponse serviceImpl(Query call, HttpServletResponse response) {
    String serviceName = call.get("serviceName", "");
    String queueName = call.get("queueName", "");
    JSONObject json = new JSONObject(true);
    if (serviceName.length() > 0 && queueName.length() > 0) {
        try {
            YaCyServices service = YaCyServices.valueOf(serviceName);
            GridQueue queue = new GridQueue(queueName);
            AvailableContainer available = Data.gridBroker.available(service, queue);
            int ac = available.getAvailable();
            String url = available.getFactory().getConnectionURL();
            if (url != null)
                json.put(ObjectAPIHandler.SERVICE_KEY, url);
            if (ac > 0) {
                // load one message and send it right again to prevent that it is lost
                MessageContainer<byte[]> message = Data.gridBroker.receive(service, queue, 3000);
                // message can be null if a timeout occurred
                if (message == null) {
                    json.put(ObjectAPIHandler.SUCCESS_KEY, false);
                    json.put(ObjectAPIHandler.COMMENT_KEY, "timeout");
                } else {
                    // send it again asap!
                    Data.gridBroker.send(service, queue, message.getPayload());
                    // evaluate whats inside
                    String payload = message.getPayload() == null ? null : new String(message.getPayload(), StandardCharsets.UTF_8);
                    JSONObject payloadjson = payload == null ? null : new JSONObject(new JSONTokener(payload));
                    json.put(ObjectAPIHandler.AVAILABLE_KEY, ac);
                    json.put(ObjectAPIHandler.MESSAGE_KEY, payloadjson == null ? new JSONObject() : payloadjson);
                    json.put(ObjectAPIHandler.SUCCESS_KEY, true);
                }
            } else {
                json.put(ObjectAPIHandler.AVAILABLE_KEY, 0);
                json.put(ObjectAPIHandler.SUCCESS_KEY, true);
            }
        } catch (IOException e) {
            json.put(ObjectAPIHandler.SUCCESS_KEY, false);
            json.put(ObjectAPIHandler.COMMENT_KEY, e.getMessage());
        }
    } else {
        json.put(ObjectAPIHandler.SUCCESS_KEY, false);
        json.put(ObjectAPIHandler.COMMENT_KEY, "the request must contain a serviceName and a queueName");
    }
    return new ServiceResponse(json);
}
Also used : JSONTokener(org.json.JSONTokener) AvailableContainer(net.yacy.grid.io.messages.AvailableContainer) ServiceResponse(net.yacy.grid.http.ServiceResponse) JSONObject(org.json.JSONObject) GridQueue(net.yacy.grid.io.messages.GridQueue) YaCyServices(net.yacy.grid.YaCyServices) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 YaCyServices (net.yacy.grid.YaCyServices)1 ServiceResponse (net.yacy.grid.http.ServiceResponse)1 AvailableContainer (net.yacy.grid.io.messages.AvailableContainer)1 GridQueue (net.yacy.grid.io.messages.GridQueue)1 JSONObject (org.json.JSONObject)1 JSONTokener (org.json.JSONTokener)1