Search in sources :

Example 6 with BRFSException

use of com.bonree.brfs.common.exception.BRFSException 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

BRFSException (com.bonree.brfs.common.exception.BRFSException)6 HttpResponse (com.bonree.brfs.common.net.http.client.HttpResponse)6 URIBuilder (com.bonree.brfs.common.net.http.client.URIBuilder)6 Service (com.bonree.brfs.common.service.Service)6 IOException (java.io.IOException)6 URI (java.net.URI)4 StorageNameStick (com.bonree.brfs.client.StorageNameStick)1 ReturnCode (com.bonree.brfs.common.ReturnCode)1 DataItem (com.bonree.brfs.common.write.data.DataItem)1 WriteDataMessage (com.bonree.brfs.common.write.data.WriteDataMessage)1 HashMap (java.util.HashMap)1