Skip to content

Commit

Permalink
Added class QBeanAsyncSupport
Browse files Browse the repository at this point in the history
Signed-off-by: Bojan Momcilov <[email protected]>
  • Loading branch information
bmomcilov committed Sep 22, 2021
1 parent c01a59a commit 9605c13
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions jpos/src/main/java/org/jpos/q2/QBeanAsyncSupport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* jPOS Project [http://jpos.org]
* Copyright (C) 2000-2021 jPOS Software SRL
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.jpos.q2;

import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public abstract class QBeanAsyncSupport extends QBeanSupport {

private long startMaxWait;
private long stopMaxWait;

@Override
protected final void startService() throws Exception {
ScheduledFuture<?> future = getScheduledThreadPoolExecutor().schedule(() -> {
try {
doStart();
} catch (Exception e) {
setState(QBean.FAILED);
log.error(e);
}
}, 0, TimeUnit.MILLISECONDS);
if (startMaxWait > 0) {
future.get(startMaxWait, TimeUnit.MILLISECONDS);
}
}

protected abstract void doStart() throws Exception;

@Override
protected final void stopService() throws Exception {
ScheduledFuture<?> future = getScheduledThreadPoolExecutor().schedule(() -> {
try {
doStop();
} catch (Exception e) {
log.error(e);
}
}, 0, TimeUnit.MILLISECONDS);
if (stopMaxWait > 0) {
future.get(stopMaxWait, TimeUnit.MILLISECONDS);
}
}

protected abstract void doStop() throws Exception;

@Override
public void setConfiguration(Configuration cfg) throws ConfigurationException {
super.setConfiguration(cfg);
startMaxWait = cfg.getLong("startMaxWait", 0L);
stopMaxWait = cfg.getLong("stopMaxWait", 0L);
}
}

0 comments on commit 9605c13

Please sign in to comment.