| <div class="docbook"><div class="navheader"><table summary="Navigation header" width="100%"><tr><th align="center" colspan="3">C.2. ConnectAttemptListener</th></tr><tr><td align="left" width="20%"><a accesskey="p" href="JMS-Client-0-8-Appendix-JMS-Extensions.html">Prev</a> </td><th align="center" width="60%">Appendix C. JMS Extensions</th><td align="right" width="20%"> <a accesskey="n" href="JMS-Client-0-8-Appendix-JMS-Extensions-Queue.html">Next</a></td></tr></table><hr /></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="JMS-Client-0-8-Appendix-JMS-Extensions-ConnectAttemptListener"></a>C.2. ConnectAttemptListener</h2></div></div></div><p>An implementation of <span class="emphasis"><em>ConnectAttemptListener</em></span> can be set on <span class="emphasis"><em>AMQConnectionFactory</em></span> |
| or <span class="emphasis"><em>PooledConnectionFactory</em></span> via <span class="emphasis"><em>#setConnectAttemptListener(ConnectAttemptListener)</em></span> |
| in order to notify messaging application about every successful |
| and unsuccessful connectivity attempt. |
| </p><p> |
| The failed attempt notification can be used as a trigger to rotate expired credentials, |
| if those are set using <a class="link" href="JMS-Client-0-8-Appendix-JMS-Extensions.html#JMS-Client-0-8-Appendix-JMS-Extensions-Connection" title="C.1. Connection extensions">connection extensions</a>. |
| The implementation can examine the error code reported as part of JMSException, and, if the error code corresponds |
| to authentication failure codes ("530" is reported by AMQP 0-8..0-91, "320" is reported by AMQP 0-10), the |
| credentials could be swapped with new ones using the connection extension mechanism. |
| See <a class="xref" href="JMS-Client-0-8-Appendix-JMS-Extensions.html#JMS-Client-0-8-Appendix-JMS-Extensions-Connection" title="C.1. Connection extensions">Section C.1, “Connection extensions”</a> for details. |
| </p><p>The following implementation of <span class="emphasis"><em>ConnectAttemptListener</em></span> illustrate the idea.</p><div class="example"><a id="d0e3366"></a><p class="title"><strong>Example C.2. Rotate credentials on authentication failure</strong></p><div class="example-contents"><pre class="programlisting"> |
| class CredentialsRotatingListener implements ConnectAttemptListener |
| { |
| |
| @Override |
| public boolean connectAttemptFailed(final URI brokerURI, final JMSException e) |
| { |
| boolean reattempt = "530".equals(e.getErrorCode()) || "320".equals(e.getErrorCode()); |
| if (reattempt) |
| { |
| rotateCredentials(brokerURI); |
| } |
| return reattempt; |
| } |
| |
| @Override |
| public void connectAttemptSucceeded(final URI brokerURI) |
| { |
| credentialsRotatedSuccessfully(brokerURI); |
| } |
| |
| private void rotateCredentials(inal URI brokerURI) |
| { |
| // credential rotating logic |
| } |
| |
| private void credentialsRotatedSuccessfully(final URI brokerURI) |
| { |
| // notify that credentials have been rotated successfully |
| } |
| } |
| </pre></div></div><br class="example-break" /><p> |
| The method <span class="emphasis"><em>connectAttemptFailed</em></span> can return true, if connection attempt needs to be repeated |
| to the same broker immediately and without incrementing a failover re-try counter. |
| Otherwise, the connection would be attempted as per failover settings. |
| </p></div><div class="navfooter"><hr /><table summary="Navigation footer" width="100%"><tr><td align="left" width="40%"><a accesskey="p" href="JMS-Client-0-8-Appendix-JMS-Extensions.html">Prev</a> </td><td align="center" width="20%"><a accesskey="u" href="JMS-Client-0-8-Appendix-JMS-Extensions.html">Up</a></td><td align="right" width="40%"> <a accesskey="n" href="JMS-Client-0-8-Appendix-JMS-Extensions-Queue.html">Next</a></td></tr><tr><td align="left" valign="top" width="40%">Appendix C. JMS Extensions </td><td align="center" width="20%"><a accesskey="h" href="JMS-Client-Book.html">Home</a></td><td align="right" valign="top" width="40%"> C.3. Queue Management</td></tr></table></div></div> |