보통 소켓을 다루는 간단한 자바 애플리케이션 예시의 경우, socket을 close하지 않아도 자연스럽게 정리된다.
공식 RabbitMQ 자바 Client을 사용할 때
publish 코드에서 사용하는 connection을 종료하지 않으면 계속 hang된다.
val connection = connectionFactory.newConnection
val channel = connection.createChannel
channel.exchangeDeclare(exchange, builtinExchangeType, false)
messages.foreach { message =>
channel.basicPublish(exchange, "", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8"))
}
jstack을 통해 비동기 쓰레드 폴링을 확인할 수 있다.
"Process Proxy: RabbitPublisher" #473 prio=6 os_prio=31 tid=0x00007f854bd53000 nid=0x1572b runnable [0x000070000cde6000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.KQueue.keventPoll(Native Method)
at sun.nio.ch.KQueuePort$EventHandlerTask.poll(KQueuePort.java:196)
at sun.nio.ch.KQueuePort$EventHandlerTask.run(KQueuePort.java:276)
at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
항상 자원을 close를 처리할 필요가 있다.
val connection = connectionFactory.newConnection
val channel = connection.createChannel
channel.exchangeDeclare(exchange, builtinExchangeType, false)
messages.foreach { message =>
channel.basicPublish(exchange, "", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8"))
}
channel.close()
connection.close()