/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) 1999-2006 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/
package org.olat.core.service;
import org.olat.core.gui.control.Event;
import org.olat.core.id.OLATResourceable;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.core.util.coordinate.CoordinatorManager;
import org.olat.core.util.event.EventBus;
import org.olat.core.util.event.GenericEventListener;
import org.olat.core.util.resource.OresHelper;
/**
* Common base class for cluster single-services.
* Send a MultiUserEvent 'SingleServiceStarted' to all cluster-nodes.
* Register myself as listener for SingleServiceStarted for my service.
* No event should received, because only one service should start.
* Log error and stop services when an event for this service will be received.
*
* @author Christian Guretzki
*/
public abstract class SingleService implements GenericEventListener {
OLog log = Tracing.createLoggerFor(this.getClass());
private String serviceName;
public SingleService( String serviceName) {
this.serviceName = serviceName;
final OLATResourceable ores = OresHelper.createOLATResourceableType(SingleService.class.getCanonicalName());
EventBus eventBus = CoordinatorManager.getCoordinator().getEventBus();
// first fire event, second register as listener because we want not to receive our 'Started event'
eventBus.fireEventToListenersOf(new ServiceStartedEvent(serviceName), ores );
log.debug("fireEventToListenersOf serviceName=" + serviceName + " ores=" + ores.getResourceableTypeName() + ":" + ores.getResourceableId());
eventBus.registerFor(this, null, ores);
}
public void event(Event event) {
log.debug("receive event=" + event + " serviceName=" + serviceName);
// check: is it an event of my service
if ( event.getCommand().equals(serviceName) ) {
log.error("### FATAL Cluster Configuration Error, two single-services running in same cluster, serviceName=" + serviceName);
stop();
}
}
public abstract void stop();
}