Bluemix的Liberty for Java和Cloudant之间的连接每天抛出RuntimeError

时间:2016-05-06 04:53:18

标签: ibm-cloud websphere-liberty cloudant

我使用Bluemix的Liberty for Java创建一个应用程序来连接Cloudant。我连接到我的RESTful API时每天都发现RuntimeException。但是,我必须每天重启我的应用程序来解决这个问题。

错误显示如下:

Exception thrown by application class 'org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage:116'
java.lang.RuntimeException: org.apache.cxf.interceptor.Fault: Error retrieving server response
at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:116)
at [internal classes]
Caused by: org.apache.cxf.interceptor.Fault: Error retrieving server response
at org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:163)
... 1 more
Caused by: com.cloudant.client.org.lightcouch.CouchDbException: Error retrieving server response
at com.cloudant.client.org.lightcouch.CouchDbClient.execute(CouchDbClient.java:501)
at com.cloudant.client.org.lightcouch.CouchDbClient.executeToInputStream(CouchDbClient.java:515)
at com.cloudant.client.api.Database.findByIndex(Database.java:361)
at com.cloudant.client.api.Database.findByIndex(Database.java:321)
at th.co.gosoft.rest.TopicService.getHotTopicList(TopicService.java:82)
at sun.reflect.GeneratedMethodAccessor34.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.ibm.ws.jaxrs20.server.LibertyJaxRsServerFactoryBean.performInvocation(LibertyJaxRsServerFactoryBean.java:636)
... 1 more
Caused by: java.net.ProtocolException: Server rejected operation
at sun.net.www.protocol.http.HttpURLConnection.expect100Continue(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at com.ibm.net.ssl.www2.protocol.https.b.getOutputStream(Unknown Source)
at com.cloudant.http.HttpConnection.execute(HttpConnection.java:231)
at com.cloudant.client.org.lightcouch.CouchDbClient.execute(CouchDbClient.java:466)
... 9 more

我使用此CloudantClientMgr如下:

package th.co.gosoft.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map.Entry;
import java.util.Set;

import com.cloudant.client.api.ClientBuilder;
import com.cloudant.client.api.CloudantClient;
import com.cloudant.client.api.Database;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class CloudantClientMgr {

    private static CloudantClient cloudant = null;
    private static Database db = null;

    private static String databaseName = "go10_db";

    private static String url = "https://xxxxxxxxxx-bluemix.cloudant.com";
    private static String user = "xxxxxxxxxx-bluemix";
    private static String password = "password";

    private static void initClient() {
        if (cloudant == null) {
            synchronized (CloudantClientMgr.class) {
                if (cloudant != null) {
                    return;
                }
                try {
                    cloudant = createClient();
                } catch (MalformedURLException e) {
                    throw new RuntimeException(e.getMessage(), e);
                }
                System.out.println("cloudant : " + cloudant.serverVersion());

            }
        }
    }

    private static CloudantClient createClient() throws MalformedURLException {
        String VCAP_SERVICES = System.getenv("VCAP_SERVICES");
        String serviceName = null;
        CloudantClient client;

        if (VCAP_SERVICES != null) {
            System.out.println("VCAP_SERVICE");
            JsonObject obj = (JsonObject) new JsonParser().parse(VCAP_SERVICES);
            Entry<String, JsonElement> dbEntry = null;
            Set<Entry<String, JsonElement>> entries = obj.entrySet();
            for (Entry<String, JsonElement> eachEntry : entries) {
                if (eachEntry.getKey().toLowerCase().contains("cloudant")) {
                    dbEntry = eachEntry;
                    break;
                }
            }
            if (dbEntry == null) {
                throw new RuntimeException("Could not find cloudantNoSQLDB key in VCAP_SERVICES env variable");
            }

            obj = (JsonObject) ((JsonArray) dbEntry.getValue()).get(0);
            serviceName = (String) dbEntry.getKey();
            System.out.println("Service Name - " + serviceName);

            obj = (JsonObject) obj.get("credentials");

            user = obj.get("username").getAsString();
            password = obj.get("password").getAsString();

            client = ClientBuilder.account(user)
                    .username(user)
                    .password(password)
                    .build();

        } else {
            System.out.println("LOCAL");
            client = ClientBuilder.url(new URL(url))
                    .username(user)
                    .password(password)
                    .build();
        }

        return client;

    }

    public static Database getDB() {
        if (cloudant == null) {
            initClient();
        }

        if (db == null) {
            try {
                db = cloudant.database(databaseName, true);
            } catch (Exception e) {
                throw new RuntimeException("DB Not found", e);
            }
        }
        return db;
    }

    private CloudantClientMgr() {
    }
}

我使用此代码连接CloudantClientMgr,如下所示:

@Path("topic")
public class TopicService {

    @POST
    @Path("/post")
    @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
    public Response createTopic(TopicModel topicModel) {

        Database db = CloudantClientMgr.getDB();

        com.cloudant.client.api.model.Response response = db.save(topicModel);

        String result = response.getId();

        return Response.status(201).entity(result).build();
    }
}

Cloudant版本是2.4.2

<dependency>
  <groupId>com.cloudant</groupId>
  <artifactId>cloudant-client</artifactId>
  <version>2.4.2</version>
</dependency>

如果有人经常发现这个问题,请告诉我比我每天重启应用程序更好的解决方案。

1 个答案:

答案 0 :(得分:0)

您描述的问题听起来与version 2.4.1中已解决的问题相同。

我会检查以确保您的应用程序或类路径中没有包含多个版本的cloudant-client,并且您的应用程序确实使用的是版本2.4.2。您提供的堆栈跟踪中的行号与2.4.2的源不匹配。