marvin-we / steem-java-api-wrapper

An API Wrapper for Steem written in Java
GNU General Public License v3.0
68 stars 27 forks source link

CommunicationHandler Connection has been closed. #112

Closed nhj7 closed 7 years ago

nhj7 commented 7 years ago

My application is running with 'steemj'. After normal operation, the following message is displayed.

[2017-10-06 19:02:52] [ INFO] [communication.CommunicationHandler.onClose:105] : Connection has been closed.

After that, it does not work. The value of 'session.isOpen ()' before running 'api' is 'true'.


public static boolean checkConnection() {
        // communicationHandler
        CommunicationHandler communicationHandler = null;
        try {
            Field field = steemApiWrapper.getClass().getDeclaredField("communicationHandler");
            field.setAccessible(true);
            communicationHandler = (CommunicationHandler) field.get(steemApiWrapper);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        try {
            Field field = communicationHandler.getClass().getDeclaredField("session");
            field.setAccessible(true);
            Session session = (Session) field.get(communicationHandler);

            logger.info("[session] : "+ session.isOpen() + ", uri : " + CONFIG.getWebsocketEndpointURI());

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        return true;

    }

Does not reconnect () work?

marvin-we commented 7 years ago

Hay @nhj1002 - Thanks for reporting. I'll have a look at it asap.

nhj7 commented 7 years ago

Add the log.

[2017-10-06 19:02:49] [ INFO] [steemit.SteemApi.checkConnection:93] : [session] : true, uri : wss://steemd.steemit.com [2017-10-06 19:02:49] [ INFO] [steemit.SteemApi.getContentReplies:146] : steemApi.getContentReplies 1 [2017-10-06 19:02:52] [ INFO] [communication.CommunicationHandler.onClose:105] : Connection has been closed.

Add the code.

public static List<Discussion> getContentReplies(String account, String permLink) throws Exception {
        checkConnection();
        logger.info("steemApi.getContentReplies 1");
        final List<Discussion> replies = steemApiWrapper.getContentReplies(account, permLink);
        logger.info("steemApi.getContentReplies 2");
        /*
        for( Discussion post : replies ){
            System.out.println( post.getBody() );
        }
        */
        return replies;
    }
nhj7 commented 7 years ago

Thank you for your quick reply.

marvin-we commented 7 years ago

Thank you for your quick reply.

Thank you for reporting this - I never noticed it myself as I only run some smaller tests :'(

So the connection is closed after 1 minute without an acitivy - I'll make it possible to configure this time (#114) and implement a reconnection mechanism on top (#113) for SteemJ v0.4.0.

Future version will also support a real failover if a node is totally not available (#100)

marvin-we commented 7 years ago

Hay @nhj1002 ,

I've done some tests now and I think the current implementation is for sure not perfect, but should already be able to reconnect - Could you tell me which version of SteemJ you are working with?

marvin-we commented 7 years ago

My test looks like this:

 // Create a new apiWrapper with your config object.
            SteemJ steemJ = new SteemJ();

            try {
                Thread.sleep(120000);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            Map<Integer, AppliedOperation> accountHistory2 = steemJ.getAccountHistory(new AccountName("dez1337"), 100,
                    100);

After 60 seconds the connection is closed, but its getting reconnected when the steemJ.getAccountHistory is called.

marvin-we commented 7 years ago

114 and #113 are fixed on the master branch - You can now configure the socketTimeout in the SteemJConfig object. If set <= 0, SteemJ will never close the connection on its own and even if this happens, the reconenction handler will recreate it immediatly :)

Please let me know if this solves your problem

nhj7 commented 7 years ago

I use 0.3.4

marvin-we commented 7 years ago

Thank you for the quick reply :)

Hm :( The implementation of 0.3.4 should also already cover this - I am quite optimistic to finish version 0.4.0 this weekend - If you still face the same issue with the new version please comment here once again and I'll reopen the issue.