/** * Constructor. * @param reconfigurable * @param messagingProperties */ protected RabbitMqClient( ReconfigurableClient<?> reconfigurable, Map<String,String> messagingProperties ) { this( reconfigurable, messagingProperties, reconfigurable.getOwnerKind()); }
/** * Constructor. */ protected ReconfigurableClient() { resetInternalClient(); // Try to find the MessagingClientFactoryRegistry service. setRegistry( lookupMessagingClientFactoryRegistryService( new OsgiHelper())); }
@Override public void removeMessagingClientFactory( IMessagingClientFactory factory ) { String factoryType = factory != null ? factory.getType() : null; IMessagingClient oldClient = null; synchronized( this ) { if( this.messagingClient != null && this.messagingClient.getMessagingType().equals( factoryType )) { // This is the messaging factory we were using... // We must release our messaging client right now. oldClient = resetInternalClient(); } } terminateClient( oldClient, "The previous client could not be terminated correctly.", this.logger ); this.logger.fine( "A messaging factory was removed: " + factoryType ); }
JmxWrapperForMessagingClient newMessagingClient = null; try { IMessagingClient rawClient = createMessagingClient( factoryName ); if( rawClient != null ) { newMessagingClient = new JmxWrapperForMessagingClient( rawClient ); newMessagingClient.setMessageQueue( this.messageProcessor.getMessageQueue()); openConnection( newMessagingClient ); this.messagingClient = newMessagingClient; else resetInternalClient(); terminateClient( oldClient, "The previous client could not be terminated correctly.", this.logger );
private void resetClients( boolean shutdown ) { // Make fresh snapshots of the clients, as we don't want to reconfigure them while holding the lock. final ArrayList<RabbitMqClient> clients; synchronized( this ) { // Get the snapshot. clients = new ArrayList<>( this.clients ); // Remove the clients, new ones will be created if necessary. this.clients.clear(); } // Now reconfigure all the clients. for( RabbitMqClient client : clients ) { try { final ReconfigurableClient<?> reconfigurable = client.getReconfigurableClient(); // The reconfigurable can never be null. // If it was, a NPE would have been thrown when the Rabbit MQ client was created. if( shutdown ) reconfigurable.closeConnection(); else reconfigurable.switchMessagingType( RabbitMqConstants.FACTORY_RABBITMQ ); } catch( Throwable t ) { // Warn but continue to reconfigure the next clients! this.logger.warning("A client has thrown an exception on reconfiguration: " + client); Utils.logException(this.logger, new RuntimeException(t)); } } }
@After public void releaseClients() throws Exception { for( ReconfigurableClient<?> client : this.clients ) { client.getMessageProcessor().stopProcessor(); client.getMessageProcessor().interrupt(); client.closeConnection(); } this.clients.clear(); }
@Test public void testTerminateClient_2() throws Exception { Logger logger = Logger.getLogger( getClass().getName()); JmxWrapperForMessagingClient client = Mockito.mock( JmxWrapperForMessagingClient.class ); ReconfigurableClient.terminateClient( client, "", logger ); Mockito.verify( client ).closeConnection(); Mockito.verify( client ).unregisterService();; Mockito.verifyNoMoreInteractions( client ); }
@Test public void testLookupMessagingClientFactoryRegistryService_noReference() { BundleContext bundleCtx = Mockito.mock( BundleContext.class ); OsgiHelper osgiHelper = Mockito.mock( OsgiHelper.class ); Mockito.when( osgiHelper.findBundleContext()).thenReturn( bundleCtx ); MessagingClientFactoryRegistry registry = ReconfigurableClient.lookupMessagingClientFactoryRegistryService( osgiHelper ); Assert.assertNull( registry ); }
@Override public void addMessagingClientFactory( final IMessagingClientFactory factory ) { this.logger.fine( "A new messaging factory was added: " + factory.getType()); synchronized( this ) { if( this.messagingClient.isDismissClient() && factory.getType().equals( this.messagingType )) { // This is the messaging factory we were expecting... // We can try to switch to this incoming factory right now! switchMessagingType( this.messagingType ); } } }
/** * Associates a message processor with this instance. * <p> * The message processor cannot be started before. This method will start it. * </p> * <p> * This method must be invoked only once. * </p> * @param messageProcessor the message processor */ public void associateMessageProcessor( AbstractMessageProcessor<T> messageProcessor ) { if( this.messageProcessor != null ) throw new IllegalArgumentException( "The message processor was already defined." ); this.messageProcessor = messageProcessor; configureMessageProcessor( messageProcessor ); this.messageProcessor.start(); }
/** * Creates a new messaging client. * @param factoryName the factory name (see {@link MessagingConstants}) * @return a new messaging client, or {@code null} if {@code factoryName} is {@code null} or cannot be found in the * available messaging factories. * @throws IOException if something went wrong */ protected IMessagingClient createMessagingClient( String factoryName ) throws IOException { IMessagingClient client = null; MessagingClientFactoryRegistry registry = getRegistry(); if( registry != null ) { IMessagingClientFactory factory = registry.getMessagingClientFactory(factoryName); if( factory != null ) client = factory.createClient( this ); } return client; }
JmxWrapperForMessagingClient newMessagingClient = null; try { IMessagingClient rawClient = createMessagingClient( factoryName ); if( rawClient != null ) { newMessagingClient = new JmxWrapperForMessagingClient( rawClient ); newMessagingClient.setMessageQueue( this.messageProcessor.getMessageQueue()); openConnection( newMessagingClient ); this.messagingClient = newMessagingClient; else resetInternalClient(); terminateClient( oldClient, "The previous client could not be terminated correctly.", this.logger );
@Test public void testTerminateClient_3() throws Exception { Logger logger = Logger.getLogger( getClass().getName()); JmxWrapperForMessagingClient client = Mockito.mock( JmxWrapperForMessagingClient.class ); Mockito.doThrow( new IOException( "for test" )).when( client ).closeConnection(); ReconfigurableClient.terminateClient( client, "", logger ); Mockito.verify( client ).closeConnection(); Mockito.verify( client ).unregisterService();; Mockito.verifyNoMoreInteractions( client ); }
@Test @SuppressWarnings( "unchecked" ) public void testLookupMessagingClientFactoryRegistryService_withReference() { BundleContext bundleCtx = Mockito.mock( BundleContext.class ); OsgiHelper osgiHelper = Mockito.mock( OsgiHelper.class ); Mockito.when( osgiHelper.findBundleContext()).thenReturn( bundleCtx ); ServiceReference<MessagingClientFactoryRegistry> reference = Mockito.mock( ServiceReference.class ); Mockito.when( bundleCtx.getServiceReference( MessagingClientFactoryRegistry.class )).thenReturn( reference ); MessagingClientFactoryRegistry registryMock = Mockito.mock( MessagingClientFactoryRegistry.class ); Mockito.when( bundleCtx.getService( reference )).thenReturn( registryMock ); MessagingClientFactoryRegistry registry = ReconfigurableClient.lookupMessagingClientFactoryRegistryService( osgiHelper ); Assert.assertNotNull( registry ); Assert.assertEquals( registryMock, registry ); }
@Override public void addMessagingClientFactory( final IMessagingClientFactory factory ) { this.logger.fine( "A new messaging factory was added: " + factory.getType()); synchronized( this ) { if( this.messagingClient.isDismissClient() && factory.getType().equals( this.messagingType )) { // This is the messaging factory we were expecting... // We can try to switch to this incoming factory right now! switchMessagingType( this.messagingType ); } } }
/** * Associates a message processor with this instance. * <p> * The message processor cannot be started before. This method will start it. * </p> * <p> * This method must be invoked only once. * </p> * @param messageProcessor the message processor */ public void associateMessageProcessor( AbstractMessageProcessor<T> messageProcessor ) { if( this.messageProcessor != null ) throw new IllegalArgumentException( "The message processor was already defined." ); this.messageProcessor = messageProcessor; configureMessageProcessor( messageProcessor ); this.messageProcessor.start(); }
/** * Creates a new messaging client. * @param factoryName the factory name (see {@link MessagingConstants}) * @return a new messaging client, or {@code null} if {@code factoryName} is {@code null} or cannot be found in the * available messaging factories. * @throws IOException if something went wrong */ protected IMessagingClient createMessagingClient( String factoryName ) throws IOException { IMessagingClient client = null; MessagingClientFactoryRegistry registry = getRegistry(); if( registry != null ) { IMessagingClientFactory factory = registry.getMessagingClientFactory(factoryName); if( factory != null ) client = factory.createClient( this ); } return client; }
/** * Constructor. */ protected ReconfigurableClient() { resetInternalClient(); // Try to find the MessagingClientFactoryRegistry service. setRegistry( lookupMessagingClientFactoryRegistryService( new OsgiHelper())); }
@Override public void removeMessagingClientFactory( IMessagingClientFactory factory ) { String factoryType = factory != null ? factory.getType() : null; IMessagingClient oldClient = null; synchronized( this ) { if( this.messagingClient != null && this.messagingClient.getMessagingType().equals( factoryType )) { // This is the messaging factory we were using... // We must release our messaging client right now. oldClient = resetInternalClient(); } } terminateClient( oldClient, "The previous client could not be terminated correctly.", this.logger ); this.logger.fine( "A messaging factory was removed: " + factoryType ); }
@Override public IMessagingClient createClient( final ReconfigurableClient<?> parent ) { this.logger.fine( "Creating a new Rabbit MQ client with owner = " + parent.getOwnerKind()); // The parent cannot be null. A NPE MUST be thrown otherwise. // That's what the RabbitMqClient constructor does. There is unit test for this. final RabbitMqClient client = new RabbitMqClient( parent, this.configuration ); synchronized( this ) { this.clients.add( client ); } this.logger.finer( "A new Rabbit MQ client was created." ); return client; }