001/* 002 * jPOS Project [http://jpos.org] 003 * Copyright (C) 2000-2026 jPOS Software SRL 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jpos.q2; 020 021import org.jpos.core.Configuration; 022import org.jpos.core.ConfigurationException; 023 024import java.util.concurrent.ScheduledFuture; 025import java.util.concurrent.TimeUnit; 026 027/** 028 * Base class for QBeans whose start/stop lifecycle should run on a background 029 * thread, optionally with a bounded wait controlled by the {@code startMaxWait} 030 * and {@code stopMaxWait} configuration properties. 031 */ 032public abstract class QBeanAsyncSupport extends QBeanSupport { 033 /** Default constructor; no instance state to initialise. */ 034 protected QBeanAsyncSupport() {} 035 036 private long startMaxWait; 037 private long stopMaxWait; 038 039 @Override 040 protected final void startService() throws Exception { 041 ScheduledFuture<?> future = getScheduledThreadPoolExecutor().schedule(() -> { 042 try { 043 doStart(); 044 } catch (Exception e) { 045 setState(QBean.FAILED); 046 log.error(e); 047 } 048 }, 0, TimeUnit.MILLISECONDS); 049 if (startMaxWait > 0) { 050 future.get(startMaxWait, TimeUnit.MILLISECONDS); 051 } 052 } 053 054 /** 055 * Performs the actual start work on the background scheduler thread. 056 * 057 * @throws Exception on failure 058 */ 059 protected abstract void doStart() throws Exception; 060 061 @Override 062 protected final void stopService() throws Exception { 063 ScheduledFuture<?> future = getScheduledThreadPoolExecutor().schedule(() -> { 064 try { 065 doStop(); 066 } catch (Exception e) { 067 log.error(e); 068 } 069 }, 0, TimeUnit.MILLISECONDS); 070 if (stopMaxWait > 0) { 071 future.get(stopMaxWait, TimeUnit.MILLISECONDS); 072 } 073 } 074 075 /** 076 * Performs the actual stop work on the background scheduler thread. 077 * 078 * @throws Exception on failure 079 */ 080 protected abstract void doStop() throws Exception; 081 082 @Override 083 public void setConfiguration(Configuration cfg) throws ConfigurationException { 084 super.setConfiguration(cfg); 085 startMaxWait = cfg.getLong("startMaxWait", 0L); 086 stopMaxWait = cfg.getLong("stopMaxWait", 0L); 087 } 088}