Pelops - 捕获连接错误?

发布于 2024-11-15 09:48:40 字数 3212 浏览 4 评论 0原文

在 Java 中使用 pelops 时有没有办法捕获连接错误?我有以下代码,但由于某种原因,我没有到达我的 catch 块。

public static Boolean testDBConnection() throws PelopsException {
    try{
        Cluster cluster = new Cluster("127.0.0.1", 9160);

        Pelops.addPool(pool, cluster, keyspace);

        Pelops.shutdown();

        return true;
    }
    catch(PelopsException e)
    {
        System.out.println("SOMETHING WENT WRONG!");
        System.out.println(e.getMessage());
        return false;
    }
}

这可能真的很简单,但我似乎无法让它发挥作用。我看到异常发生了,但我就没有办法捕捉到这个吗?有人可以引导我走向正确的方向吗?

谢谢!

编辑 - Eclipse 控制台中返回异常...

15:47:33.545 [main] DEBUG o.s.c.pelops.pool.CommonsBackedPool - Made new connection 'Connection[Testing][127.0.0.1:9160][724408050]'
15:47:34.545 [main] ERROR o.scale7.cassandra.pelops.Connection - Failed to open transport.  See cause for details...
org.apache.thrift.transport.TTransportException: java.net.ConnectException: Connection refused: connect
    at org.apache.thrift.transport.TSocket.open(TSocket.java:185) ~[libthrift-0.5.0.jar:na]
    at org.apache.thrift.transport.TFramedTransport.open(TFramedTransport.java:81) ~[libthrift-0.5.0.jar:na]
    at org.scale7.cassandra.pelops.Connection.open(Connection.java:70) ~[scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool$ConnectionFactory.makeObject(CommonsBackedPool.java:785) [scale7-pelops-0.913.jar:na]
    at org.apache.commons.pool.impl.GenericKeyedObjectPool.addObject(GenericKeyedObjectPool.java:1685) [commons-pool-1.5.5.jar:1.5.5]
    at org.apache.commons.pool.impl.GenericKeyedObjectPool.ensureMinIdle(GenericKeyedObjectPool.java:2058) [commons-pool-1.5.5.jar:1.5.5]
    at org.apache.commons.pool.impl.GenericKeyedObjectPool.preparePool(GenericKeyedObjectPool.java:1722) [commons-pool-1.5.5.jar:1.5.5]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool.addNode(CommonsBackedPool.java:373) [scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool.<init>(CommonsBackedPool.java:104) [scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool.<init>(CommonsBackedPool.java:64) [scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool.<init>(CommonsBackedPool.java:52) [scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.Pelops.addPool(Pelops.java:25) [scale7-pelops-0.913.jar:na]
    at libraries.cassandra.testDBConnection(cassandra.java:192) [classes/:na]
    at libraries.cassandra.main(cassandra.java:38) [classes/:na]
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.6.0_25]
    at java.net.PlainSocketImpl.doConnect(Unknown Source) ~[na:1.6.0_25]
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.6.0_25]
    at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.6.0_25]
    at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.6.0_25]
    at java.net.Socket.connect(Unknown Source) ~[na:1.6.0_25]
    at org.apache.thrift.transport.TSocket.open(TSocket.java:180) ~[libthrift-0.5.0.jar:na]
    ... 13 common frames omitted

Is there a way to catch a connection error when using pelops in Java? I have the following code but for some reason, I'm not getting to my catch block.

public static Boolean testDBConnection() throws PelopsException {
    try{
        Cluster cluster = new Cluster("127.0.0.1", 9160);

        Pelops.addPool(pool, cluster, keyspace);

        Pelops.shutdown();

        return true;
    }
    catch(PelopsException e)
    {
        System.out.println("SOMETHING WENT WRONG!");
        System.out.println(e.getMessage());
        return false;
    }
}

This is probably something really easy, but I can't seem to get it to work. I see the exceptions coming through, but there is just no way I can catch this? Could someone please lead me in the right direction?

Thanks!

EDIT - Exception being returned in Eclipse Console...

15:47:33.545 [main] DEBUG o.s.c.pelops.pool.CommonsBackedPool - Made new connection 'Connection[Testing][127.0.0.1:9160][724408050]'
15:47:34.545 [main] ERROR o.scale7.cassandra.pelops.Connection - Failed to open transport.  See cause for details...
org.apache.thrift.transport.TTransportException: java.net.ConnectException: Connection refused: connect
    at org.apache.thrift.transport.TSocket.open(TSocket.java:185) ~[libthrift-0.5.0.jar:na]
    at org.apache.thrift.transport.TFramedTransport.open(TFramedTransport.java:81) ~[libthrift-0.5.0.jar:na]
    at org.scale7.cassandra.pelops.Connection.open(Connection.java:70) ~[scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool$ConnectionFactory.makeObject(CommonsBackedPool.java:785) [scale7-pelops-0.913.jar:na]
    at org.apache.commons.pool.impl.GenericKeyedObjectPool.addObject(GenericKeyedObjectPool.java:1685) [commons-pool-1.5.5.jar:1.5.5]
    at org.apache.commons.pool.impl.GenericKeyedObjectPool.ensureMinIdle(GenericKeyedObjectPool.java:2058) [commons-pool-1.5.5.jar:1.5.5]
    at org.apache.commons.pool.impl.GenericKeyedObjectPool.preparePool(GenericKeyedObjectPool.java:1722) [commons-pool-1.5.5.jar:1.5.5]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool.addNode(CommonsBackedPool.java:373) [scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool.<init>(CommonsBackedPool.java:104) [scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool.<init>(CommonsBackedPool.java:64) [scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.pool.CommonsBackedPool.<init>(CommonsBackedPool.java:52) [scale7-pelops-0.913.jar:na]
    at org.scale7.cassandra.pelops.Pelops.addPool(Pelops.java:25) [scale7-pelops-0.913.jar:na]
    at libraries.cassandra.testDBConnection(cassandra.java:192) [classes/:na]
    at libraries.cassandra.main(cassandra.java:38) [classes/:na]
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.6.0_25]
    at java.net.PlainSocketImpl.doConnect(Unknown Source) ~[na:1.6.0_25]
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.6.0_25]
    at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.6.0_25]
    at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.6.0_25]
    at java.net.Socket.connect(Unknown Source) ~[na:1.6.0_25]
    at org.apache.thrift.transport.TSocket.open(TSocket.java:180) ~[libthrift-0.5.0.jar:na]
    ... 13 common frames omitted

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铃予 2024-11-22 09:48:40

抛出的异常 org.apache.thrift.TTransportException 是一个已检查的异常,未由扩展 RuntimeException 的 PelopsException 捕获和包装。

如果您希望方法始终返回 PelopsException,则必须 catch(Exception e),然后在 catch 块内抛出 new PelopsException(e.getMessage()) 。

   catch(Exception e)
{
    System.out.println("SOMETHING WENT WRONG!");
    System.out.println(e.getMessage());
    throw new PelopsException(e.getMessage());
}

The exception being thrown org.apache.thrift.TTransportException is a checked exception that is not caught and wrapped by PelopsException which extends RuntimeException.

If you want your method to always return a PelopsException, you must catch(Exception e) and then throw new PelopsException(e.getMessage()) inside your catch block.

   catch(Exception e)
{
    System.out.println("SOMETHING WENT WRONG!");
    System.out.println(e.getMessage());
    throw new PelopsException(e.getMessage());
}
凉城已无爱 2024-11-22 09:48:40

它可能会抛出 未经检查的异常 或错误(这是不是例外)。

尝试将代码更改为 catch (Throwable e) 并看看会得到什么。

It may be throwing an unchecked exception or an Error (which is not an Exception).

Try changing your code to catch (Throwable e) and see what you get.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文