001/* 002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 */ 017package org.nuxeo.ecm.core.management.standby; 018 019import javax.management.JMException; 020import org.nuxeo.ecm.core.api.NuxeoException; 021import org.nuxeo.runtime.api.Framework; 022import org.nuxeo.runtime.management.ServerLocator; 023import org.nuxeo.runtime.model.ComponentContext; 024import org.nuxeo.runtime.model.DefaultComponent; 025import org.nuxeo.runtime.trackers.concurrent.ThreadEventHandler; 026import org.nuxeo.runtime.trackers.concurrent.ThreadEventListener; 027 028import com.codahale.metrics.Counter; 029import com.codahale.metrics.Meter; 030import com.codahale.metrics.MetricRegistry; 031import com.codahale.metrics.Timer; 032import com.codahale.metrics.Timer.Context; 033 034/** 035 * @since 9.2 036 */ 037public class StandbyComponent extends DefaultComponent { 038 039 protected final StandbyCommand command = new StandbyCommand(); 040 041 protected final Counter active = Framework.getService(MetricRegistry.class) 042 .counter(MetricRegistry.name(StandbyComponent.class, "active")); 043 044 protected final Meter meter = Framework.getService(MetricRegistry.class) 045 .meter(MetricRegistry.name(StandbyComponent.class, "meter")); 046 047 protected final Timer timer = Framework.getService(MetricRegistry.class) 048 .timer(MetricRegistry.name(StandbyComponent.class, "timer")); 049 050 protected final ThreadLocal<Context> holder = new ThreadLocal<>(); 051 052 protected final ThreadEventListener threadsListener = new ThreadEventListener(new ThreadEventHandler() { 053 054 @Override 055 public void onEnter(boolean isLongRunning) { 056 if (isLongRunning) { 057 return; 058 } 059 holder.set(timer.time()); 060 meter.mark(); 061 active.inc(); 062 } 063 064 @Override 065 public void onLeave() { 066 active.dec(); 067 holder.get().close(); 068 } 069 070 }); 071 072 @Override 073 public void activate(ComponentContext context) { 074 threadsListener.install(); 075 try { 076 command.registration.with(Framework.getService(ServerLocator.class).lookupServer()).register(); 077 } catch (JMException cause) { 078 throw new NuxeoException("Cannot register standby command", cause); 079 } 080 } 081 082 @Override 083 public void deactivate(ComponentContext context) { 084 threadsListener.uninstall(); 085 try { 086 command.registration.unregister(); 087 } catch (JMException cause) { 088 throw new NuxeoException("Cannot unregister standby command", cause); 089 } 090 } 091 092}