Search in sources :

Example 1 with WriteDataMessage

use of com.bonree.brfs.common.write.data.WriteDataMessage in project BRFS by zhangnianli.

the class WriteDataMessageHandler method handle.

@Override
public void handle(HttpMessage msg, HandleResultCallback callback) {
    WriteDataMessage writeMsg;
    try {
        writeMsg = ProtoStuffUtils.deserializeThrowable(msg.getContent(), WriteDataMessage.class);
        DataItem[] items = writeMsg.getItems();
        LOG.debug("Writing DataItem[{}]", items.length);
        if (items == null || items.length == 0) {
            callback.completed(new HandleResult(true));
            return;
        }
        writer.write(writeMsg.getStorageNameId(), items, new DataWriteCallback(callback));
    } catch (Exception e) {
        LOG.error("handle write data message error", e);
        callback.completed(new HandleResult(false));
    }
}
Also used : WriteDataMessage(com.bonree.brfs.common.write.data.WriteDataMessage) DataItem(com.bonree.brfs.common.write.data.DataItem) HandleResult(com.bonree.brfs.common.net.http.HandleResult) JsonException(com.bonree.brfs.common.utils.JsonUtils.JsonException)

Example 2 with WriteDataMessage

use of com.bonree.brfs.common.write.data.WriteDataMessage in project BRFS by zhangnianli.

the class DefaultStorageNameStick method writeData.

@Override
public String[] writeData(InputItem[] itemArrays) {
    WriteDataMessage dataMessage = new WriteDataMessage();
    dataMessage.setStorageNameId(storageId);
    DataItem[] dataItems = new DataItem[itemArrays.length];
    for (int i = 0; i < dataItems.length; i++) {
        dataItems[i] = new DataItem();
        dataItems[i].setBytes(itemArrays[i].getBytes());
    }
    dataMessage.setItems(dataItems);
    try {
        Service[] serviceList = regionNodeSelector.select(regionNodeSelector.serviceNum());
        if (serviceList.length == 0) {
            throw new BRFSException("none disknode!!!");
        }
        for (Service service : serviceList) {
            URI uri = new URIBuilder().setScheme(config.getUrlSchema()).setHost(service.getHost()).setPort(service.getPort()).setPath(config.getDuplicateUrlRoot() + "/").build();
            HttpResponse response = null;
            try {
                response = client.executePost(uri, defaultHeaders, ProtoStuffUtils.serialize(dataMessage));
            } catch (Exception e) {
                LOG.warn("write data http request failed", e);
                continue;
            }
            if (response == null) {
                throw new Exception("can not get response for writeData!");
            }
            if (response.isReponseOK()) {
                List<String> results = JSON.parseArray(BrStringUtils.fromUtf8Bytes(response.getResponseBody()), String.class);
                String[] fids = new String[results.size()];
                return results.toArray(fids);
            }
        }
    } catch (Exception e) {
        LOG.error("write data error", e);
    }
    return null;
}
Also used : WriteDataMessage(com.bonree.brfs.common.write.data.WriteDataMessage) DataItem(com.bonree.brfs.common.write.data.DataItem) Service(com.bonree.brfs.common.service.Service) HttpResponse(com.bonree.brfs.common.net.http.client.HttpResponse) URI(java.net.URI) BRFSException(com.bonree.brfs.common.exception.BRFSException) IOException(java.io.IOException) URIBuilder(com.bonree.brfs.common.net.http.client.URIBuilder) BRFSException(com.bonree.brfs.common.exception.BRFSException)

Aggregations

DataItem (com.bonree.brfs.common.write.data.DataItem)2 WriteDataMessage (com.bonree.brfs.common.write.data.WriteDataMessage)2 BRFSException (com.bonree.brfs.common.exception.BRFSException)1 HandleResult (com.bonree.brfs.common.net.http.HandleResult)1 HttpResponse (com.bonree.brfs.common.net.http.client.HttpResponse)1 URIBuilder (com.bonree.brfs.common.net.http.client.URIBuilder)1 Service (com.bonree.brfs.common.service.Service)1 JsonException (com.bonree.brfs.common.utils.JsonUtils.JsonException)1 IOException (java.io.IOException)1 URI (java.net.URI)1