/** * Shutdown the guard. After this method is invoked, the database will always be considered unavailable. */ public void shutdown() { synchronized ( requirementCount ) { if ( isShutdown.getAndSet( true ) ) { return; } if ( requirementCount.get() == 0 ) { listeners.notify( AvailabilityListener::unavailable ); } } }
public void unregister( NeoStoreDataSource dataSource ) { dataSources.remove( dataSource ); dsRegistrationListeners.notify( listener -> listener.unregistered( dataSource ) ); life.remove( dataSource ); }
@Override public void require( AvailabilityRequirement requirement ) { if ( !blockingRequirements.add( requirement ) ) { return; } synchronized ( requirementCount ) { if ( requirementCount.getAndIncrement() == 0 && !isShutdown.get() ) { log.info( DATABASE_UNAVAILABLE_MSG, requirement.description(), databaseName ); listeners.notify( AvailabilityListener::unavailable ); } } }
@Override public void fulfill( AvailabilityRequirement requirement ) { if ( !blockingRequirements.remove( requirement ) ) { return; } synchronized ( requirementCount ) { if ( requirementCount.getAndDecrement() == 1 && !isShutdown.get() ) { log.info( DATABASE_AVAILABLE_MSG, requirement.description(), databaseName ); listeners.notify( AvailabilityListener::available ); } } }
public void register( NeoStoreDataSource dataSource ) { dataSources.add( dataSource ); if ( life.getStatus().equals( LifecycleStatus.STARTED ) ) { life.add( dataSource ); dsRegistrationListeners.notify( listener -> listener.registered( dataSource ) ); } }
@Test void notifyWithExecutorAndNullNotification() { assertThrows( NullPointerException.class, () -> new Listeners<Listener>().notify( newSingleThreadExecutor(), null ) ); }
@Test void notifyWithNullNotification() { assertThrows( NullPointerException.class, () -> new Listeners<>().notify( null ) ); }
@Test void notifyWithNullExecutorAndNullNotification() { assertThrows( NullPointerException.class, () -> new Listeners<>().notify( null, null ) ); }
@Test void notifyWithNotification() { String message = "foo"; Listener listener1 = new Listener(); Listener listener2 = new Listener(); Listeners<Listener> listeners = newListeners( listener1, listener2 ); listeners.notify( listener -> listener.process( message ) ); assertEquals( message, listener1.message ); assertEquals( currentThread().getName(), listener1.threadName ); assertEquals( message, listener2.message ); assertEquals( currentThread().getName(), listener2.threadName ); }
@Test void notifyWithNullExecutorAndNotification() { assertThrows( NullPointerException.class, () -> new Listeners<Listener>().notify( null, listener -> listener.process( "foo" ) ) ); }
@Test void notifyWithExecutorAndNotification() throws Exception { String message = "foo"; String threadNamePrefix = "test-thread"; Listener listener1 = new Listener(); Listener listener2 = new Listener(); Listeners<Listener> listeners = newListeners( listener1, listener2 ); ExecutorService executor = newSingleThreadExecutor( named( threadNamePrefix ) ); listeners.notify( executor, listener -> listener.process( message ) ); executor.shutdown(); executor.awaitTermination( 1, TimeUnit.MINUTES ); assertEquals( message, listener1.message ); assertThat( listener1.threadName, startsWith( threadNamePrefix ) ); assertEquals( message, listener2.message ); assertThat( listener2.threadName, startsWith( threadNamePrefix ) ); }
/** * Shutdown the guard. After this method is invoked, the database will always be considered unavailable. */ public void shutdown() { synchronized ( requirementCount ) { if ( isShutdown.getAndSet( true ) ) { return; } if ( requirementCount.get() == 0 ) { listeners.notify( AvailabilityListener::unavailable ); } } }
public void unregister( NeoStoreDataSource dataSource ) { dataSources.remove( dataSource ); dsRegistrationListeners.notify( listener -> listener.unregistered( dataSource ) ); life.remove( dataSource ); }
@Override public void require( AvailabilityRequirement requirement ) { if ( !blockingRequirements.add( requirement ) ) { return; } synchronized ( requirementCount ) { if ( requirementCount.getAndIncrement() == 0 && !isShutdown.get() ) { log.info( DATABASE_UNAVAILABLE_MSG, requirement.description(), databaseName ); listeners.notify( AvailabilityListener::unavailable ); } } }
@Override public void fulfill( AvailabilityRequirement requirement ) { if ( !blockingRequirements.remove( requirement ) ) { return; } synchronized ( requirementCount ) { if ( requirementCount.getAndDecrement() == 1 && !isShutdown.get() ) { log.info( DATABASE_AVAILABLE_MSG, requirement.description(), databaseName ); listeners.notify( AvailabilityListener::available ); } } }
public void register( NeoStoreDataSource dataSource ) { dataSources.add( dataSource ); if ( life.getStatus().equals( LifecycleStatus.STARTED ) ) { life.add( dataSource ); dsRegistrationListeners.notify( listener -> listener.registered( dataSource ) ); } }