Skip to content

Commit

Permalink
[BACKLOG-39783] - Implemented a PentahoSystemPublisher that allows ar…
Browse files Browse the repository at this point in the history
…bitrary events to be published to concerned subscribers
  • Loading branch information
wseyler committed Feb 6, 2024
1 parent b721203 commit 558c2a7
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public static boolean init( final IApplicationContext pApplicationContext, final
if ( debug ) {
Logger.debug( PentahoSystem.class, "PentahoSystem Init Complete" ); //$NON-NLS-1$
}

PentahoSystemPublisher.getInstance().publish( PentahoSystemPublisher.START_UP_TOPIC, true );
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*!
*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
*
* Copyright (c) 2002-2024 Hitachi Vantara. All rights reserved.
*
*/

package org.pentaho.platform.engine.core.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

public class PentahoSystemPublisher {
public static final String START_UP_TOPIC = "system_startup";
public static final String SHUT_DOWN_TOPIC = "system_shutdown";
private final Map<String, ArrayList<Consumer<?>>> topicsSubscribers = new HashMap<>();
private static PentahoSystemPublisher instance = null;

public static PentahoSystemPublisher getInstance() {
if ( instance == null ) {
instance = new PentahoSystemPublisher();
}
return instance;
}

public int topicCount() {
return topicsSubscribers.size();
}

public <T> void publish( String topic, T value ) {
ArrayList<Consumer<?>> subscribers = topicsSubscribers.get( topic );
if ( subscribers == null ) {
return;
}

for ( Consumer subscriberConsumer : subscribers ) {
subscriberConsumer.accept( value );
}
}

public synchronized <T> void subscribe( String topicName, Consumer<T> subscriberCallback ) {
ArrayList<Consumer<?>> subscribers = topicsSubscribers.get( topicName );
if ( subscribers == null ) {
subscribers = new ArrayList<>();
subscribers.add( subscriberCallback );
topicsSubscribers.put( topicName, subscribers );
} else {
subscribers.add( subscriberCallback );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.pentaho.platform.engine.core.system;

import org.junit.Assert;
import org.junit.Test;

public class PentahoSystemPublisherTest {
@Test
public void subscribeTest() {
PentahoSystemPublisher.getInstance().subscribe( PentahoSystemPublisher.START_UP_TOPIC, this::startupSubscriber );
PentahoSystemPublisher.getInstance().subscribe( PentahoSystemPublisher.SHUT_DOWN_TOPIC, this::shutdownSubscriber );
Assert.assertEquals( 2, PentahoSystemPublisher.getInstance().topicCount() );
PentahoSystemPublisher.getInstance().publish( PentahoSystemPublisher.START_UP_TOPIC, true );
PentahoSystemPublisher.getInstance().publish( PentahoSystemPublisher.SHUT_DOWN_TOPIC, true );
}

private void shutdownSubscriber( boolean isStopping ) {
Assert.assertTrue( isStopping );
}

public void startupSubscriber( boolean isStarting ) {
Assert.assertTrue( isStarting );
}
}

0 comments on commit 558c2a7

Please sign in to comment.