{"id":6085,"date":"2016-12-16T10:10:35","date_gmt":"2016-12-16T15:10:35","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=6085"},"modified":"2016-12-16T10:15:50","modified_gmt":"2016-12-16T15:15:50","slug":"is-a-jms-messageconsumer-truly-asynchronousnon-blocking","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2016\/12\/16\/is-a-jms-messageconsumer-truly-asynchronousnon-blocking\/","title":{"rendered":"Is a JMS MessageConsumer truly asynchronous\/non-blocking?"},"content":{"rendered":"<p>Yes.<\/p>\n<p>The code tree is as follows:<\/p>\n<p>Receive call on the ActiveMQMessageConsumer class, which implements the javax.jms.MessageConsumer interface&#8230;<\/p>\n<pre>\r\n  public javax.jms.Message receive()\r\n    throws JMSException\r\n  {\r\n    checkClosed();\r\n    checkMessageListener();\r\n    \r\n    sendPullCommand(0L);\r\n    MessageDispatch md = dequeue(-1L);\r\n    if (md == null) {\r\n      return null;\r\n    }\r\n    beforeMessageIsConsumed(md);\r\n    afterMessageIsConsumed(md, false);\r\n    \r\n    return createActiveMQMessage(md);\r\n  }\r\n<\/pre>\n<p>The sendPullCommand() method, in the same class, is shown below&#8230;<\/p>\n<pre>\r\nprotected void sendPullCommand(long timeout)\r\n    throws JMSException\r\n  {\r\n    clearDeliveredList();\r\n    if ((this.info.getCurrentPrefetchSize() == 0) && (this.unconsumedMessages.isEmpty()))\r\n    {\r\n      MessagePull messagePull = new MessagePull();\r\n      messagePull.configure(this.info);\r\n      messagePull.setTimeout(timeout);\r\n      this.session.asyncSendPacket(messagePull);\r\n    }\r\n  }\r\n<\/pre>\n<p>The MessagePull{} class is configured and sent to the broker with the asyncSendPacket() method call.<\/p>\n<p>For empirical evidence, an actual strace of a consumer registered with ActiveMQ shows what is below.<\/p>\n<p>We identified the socket associated with the file descriptor 430 using strace against the Consumer.  What is below is the strace against ActiveMQ itself.  We start our consumer and create a Queue connection to the broker at 9:17:05.  At 9:17:17, we send a message from our producer to the queue.  There is no other traffic between the consumer and ActiveMQ until ActiveMQ sends it the message at 9:17:17.  It doesn&#8217;t even contain a poll call every so often to see if there is work to do.  As a result, it is truly asynchronous.<\/p>\n<pre>\r\n15032 09:17:05.722665 fcntl(430, F_GETFL <unfinished ...>\r\n15032 09:17:05.722741 fcntl(430, F_SETFL, O_RDWR <unfinished ...>\r\n30525 09:17:05.724339 setsockopt(430, SOL_SOCKET, SO_RCVBUF, [65536], 4) = 0\r\n30525 09:17:05.724409 setsockopt(430, SOL_SOCKET, SO_SNDBUF, [65536], 4) = 0\r\n30525 09:17:05.725299 sendto(430, \"ActiveMQ MaxFrameSize CacheSize CacheEnabled SizePrefixDisabled MaxInactivityDurationInitalDelay TcpNoDelayEnabled MaxInactivityDuration TightEncodingEnabled StackTraceEnabled\", 244, 0, NULL, 0 <unfinished ...>\r\n30657 09:17:05.731351 setsockopt(430, SOL_TCP, TCP_NODELAY, [1], 4 <unfinished ...>\r\n30658 09:17:05.738382 sendto(430, \"ID:cmhlcarchapp01.foo.com-18813-1481762261285-0:1 (tcp:\/\/cmhlcarchapp01.foo.com:61616 amq2\", 121, 0, NULL, 0) = 121\r\n30658 09:17:05.807018 sendto(430, \"\", 16, 0, NULL, 0) = 16\r\n30657 09:17:05.807576 sendto(430, \"\", 15, 0, NULL, 0) = 15\r\n30657 09:17:05.807658 recvfrom(430, \"ID:cmhlcarchapp01.foo.com-15477-1481897825526-1:1 ActiveMQ.Advisory.TempQueue,ActiveMQ.Advisory.TempTopic\", 8192, 0, NULL, NULL) = 153\r\n30657 09:17:05.817916 sendto(430, \"\", 15, 0, NULL, 0) = 15\r\n30657 09:17:05.852143 recvfrom(430, \"} ID:cmhlcarchapp01.foo.com-15477-1481897825526-1:1 requestQ ID:cmhlcarchapp01.expressco.com-15477-1481897825526-0:1\", 8192, 0, NULL, NULL) = 157\r\n30657 09:17:05.865215 sendto(430, \"\", 15, 0, NULL, 0 <unfinished ...>\r\n30657 09:17:05.874460 sendto(430, \"\", 15, 0, NULL, 0 <unfinished ...>\r\n30658 09:17:17.179415 sendto(430, \"ID:cmhlcarchapp01.foo.com-15477-1481897825526-1:1 requestQ {ID:cmhlcarchapp01.foo.com-63166-1481897836921-1:1 replyQ Y Hello world. Y Y {\", 247, 0, NULL, 0) = 247\r\n30657 09:17:17.202377 sendto(430, \"\", 15, 0, NULL, 0) = 15\r\n30657 09:17:17.202469 recvfrom(430, \"ID:cmhlcarchapp01.foo.com-63166-1481897836921-1:1:1:1:1 Y Hello world.\", 8192, 0, NULL, NULL) = 133\r\n30657 09:17:17.206053 sendto(430, \"\", 15, 0, NULL, 0 <unfinished ...>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Yes. The code tree is as follows: Receive call on the ActiveMQMessageConsumer class, which implements the javax.jms.MessageConsumer interface&#8230; public javax.jms.Message receive() throws JMSException { checkClosed(); checkMessageListener(); sendPullCommand(0L); MessageDispatch md = dequeue(-1L); if (md == null) { return null; } beforeMessageIsConsumed(md);&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2016\/12\/16\/is-a-jms-messageconsumer-truly-asynchronousnon-blocking\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[79,7,44,62],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6085"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=6085"}],"version-history":[{"count":7,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6085\/revisions"}],"predecessor-version":[{"id":6093,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/6085\/revisions\/6093"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=6085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=6085"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=6085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}