-
Notifications
You must be signed in to change notification settings - Fork 723
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5518 from wseyler/BACKLOG-39783
[BACKLOG-39783] - Implemented a PentahoSystemPublisher that allows ar…
- Loading branch information
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
core/src/main/java/org/pentaho/platform/engine/core/system/PentahoSystemPublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
core/src/test/java/org/pentaho/platform/engine/core/system/PentahoSystemPublisherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |