-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
161 additions
and
77 deletions.
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
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
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
33 changes: 33 additions & 0 deletions
33
bundle/src/main/java/com/adobe/acs/commons/util/ClusterLeader.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,33 @@ | ||
/* | ||
* #%L | ||
* ACS AEM Commons Bundle | ||
* %% | ||
* Copyright (C) 2021 Adobe | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package com.adobe.acs.commons.util; | ||
|
||
import org.apache.sling.discovery.DiscoveryService; | ||
import org.apache.sling.discovery.InstanceDescription; | ||
|
||
/** | ||
* Marker interface. | ||
* Available as OSGi service only on instances being the cluster leader. | ||
* @see InstanceDescription#isLeader | ||
* @see DiscoveryService#getTopology | ||
*/ | ||
public interface ClusterLeader { | ||
|
||
} |
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
89 changes: 89 additions & 0 deletions
89
bundle/src/main/java/com/adobe/acs/commons/util/impl/DiscoveryServiceHelper.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,89 @@ | ||
/* | ||
* #%L | ||
* ACS AEM Commons Bundle | ||
* %% | ||
* Copyright (C) 2021 Adobe | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
package com.adobe.acs.commons.util.impl; | ||
|
||
import org.apache.sling.discovery.DiscoveryService; | ||
import org.apache.sling.discovery.TopologyEvent; | ||
import org.apache.sling.discovery.TopologyEventListener; | ||
import org.apache.sling.discovery.TopologyView; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.framework.ServiceRegistration; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Deactivate; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
import com.adobe.acs.commons.util.ClusterLeader; | ||
|
||
/** | ||
* This component registers a {@link ClusterLeader} service in case the current topology instance is the leader and | ||
* unregisters that service if that is no longer the case. | ||
* | ||
* Components which are only supposed to be executed on the cluster leader can depend on the | ||
* service with marker interface {@link ClusterLeader}. | ||
*/ | ||
@Component | ||
public class DiscoveryServiceHelper implements TopologyEventListener { | ||
|
||
@Reference | ||
DiscoveryService discoveryService; | ||
|
||
private BundleContext bundleContext; | ||
|
||
private ServiceRegistration<ClusterLeader> clusterLeaderServiceRegistration; | ||
|
||
@Override | ||
public void handleTopologyEvent(TopologyEvent event) { | ||
TopologyView newView = event.getNewView(); | ||
if (newView != null) { | ||
if (newView.getLocalInstance().isLeader()) { | ||
registerClusterLeader(); | ||
} else { | ||
unregisterClusterLeader(); | ||
} | ||
} | ||
} | ||
|
||
@Activate | ||
public void activate(BundleContext bundleContext) { | ||
this.bundleContext = bundleContext; | ||
if (discoveryService.getTopology().getLocalInstance().isLeader()) { | ||
registerClusterLeader(); | ||
} | ||
} | ||
|
||
@Deactivate | ||
public void deactivate() { | ||
unregisterClusterLeader(); | ||
} | ||
|
||
private synchronized void registerClusterLeader() { | ||
if (clusterLeaderServiceRegistration == null) { | ||
clusterLeaderServiceRegistration = bundleContext.registerService(ClusterLeader.class, new ClusterLeader() {}, null); | ||
} | ||
} | ||
|
||
private synchronized void unregisterClusterLeader() { | ||
if (clusterLeaderServiceRegistration != null) { | ||
clusterLeaderServiceRegistration.unregister(); | ||
clusterLeaderServiceRegistration = null; | ||
} | ||
} | ||
} |
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
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
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