line-bot-sdk-java icon indicating copy to clipboard operation
line-bot-sdk-java copied to clipboard

Java ConsoleApplication stops after calling the APIs after 60 seconds delay.

Open veryhungrycaterpillar opened this issue 4 years ago • 2 comments

Bug Report

Describe the bug I made Java ConsoleApplication for send PushMessage using line-bot-sdk-java. After Sending PushMessage, Java process didn't stop. Java process stopped 1 minutes later after sending. Java process stopped soon if not sending.

To Reproduce

I send PushMessage by following code.

public void send(List<PushMessage> pushMessages) {

	if (pushMessages.isEmpty()) {
		return;
	}

	LineMessagingClient client = LineMessagingClient
			.builder(this.channelAccessToken)
			.build();

	for (PushMessage pushMessage : pushMessages) {
		try {
			BotApiResponse botApiResponse
					= client.pushMessage(pushMessage).get();
		} catch (InterruptedException | ExecutionException ex) {
			throw new RuntimeException(ex);
		}
	}
}

Expected behavior Java process stops soon after sending.

Environment (please complete the following information):

  • OS: Windows10, CentOS 6.7 (64bit)
  • JDK Version oracle jdk-8u92
  • line-bot-sdk-java version(s) 4.3.0

Additional context Does Response close in LineMessagingClientImpl? https://github.com/line/line-bot-sdk-java/blob/master/line-bot-api-client/src/main/java/com/linecorp/bot/client/LineMessagingClientImpl.java

Retrofit added comment about close Response in 2020 May. https://github.com/square/retrofit/pull/3400

veryhungrycaterpillar avatar Feb 09 '21 03:02 veryhungrycaterpillar

Thank you for your reporting. I can reproduce the issue.

Looks like it's due to retrofit's internal executor service is based on user thread (not daemon thread), It's blocks JVM shutdown.

kazuki-ma avatar Feb 09 '21 03:02 kazuki-ma

https://square.github.io/okhttp/3.x/okhttp/okhttp3/OkHttpClient.html

OkHttpClient closes threads after about 60 seconds of idling. It's unexpected behaviour, but it's not a serious bug.

import com.linecorp.bot.client.LineMessagingClient;
import com.linecorp.bot.model.response.BotInfoResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class MyApplication {
    private static final Logger log = LoggerFactory.getLogger(MyApplication.class);

    public static void main(String[] args) throws Exception {
        LineMessagingClient client = LineMessagingClient
                .builder("< channelAccessToken >")
                .build();

        BotInfoResponse botInfoResponse = client.getBotInfo().get();

        log.info("Got response: {}", botInfoResponse);
        long finished = System.currentTimeMillis();
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            log.info("Shutdown... after {} seconds delay",
                    (System.currentTimeMillis() - finished)/1000);
        }));
    }
}
18:57:46.242 [Thread-1] INFO MyApplication - Shutdown... after 60 seconds delay

line-bot-sdk-java should provide a way to call client.connectionPool().evictAll();.

tokuhirom avatar Dec 08 '21 10:12 tokuhirom

This is a spec.

tokuhirom avatar Aug 01 '23 02:08 tokuhirom