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.transaction.participant;
020
021import java.io.Serializable;
022import java.util.Optional;
023import java.util.Random;
024
025import org.jpos.core.Configurable;
026import org.jpos.core.Configuration;
027import org.jpos.core.ConfigurationException;
028import org.jpos.transaction.AbortParticipant;
029import org.jpos.transaction.Context;
030
031/**
032 * Delay participant
033 *
034 * Can be used for debugging/testing purposes. It just delays the transaction callbacks.
035 *
036 */
037public class Delay implements AbortParticipant, Configurable {
038    long prepareDelay = 0L;
039    long commitDelay = 0L;
040    long abortDelay = 0L;
041    Random random;
042    String delayName;
043
044    public int prepare(long id, Serializable context) {
045        long delay = prepareDelay;
046        if (context instanceof Context) {
047            Context ctx = (Context) context;
048            delay = Optional.ofNullable((Long) ctx.get(delayName)).orElse(delay);
049            ctx.checkPoint(" pre-delay " + delay);
050            sleep (delay);
051            ctx.checkPoint("post-delay " + delay);
052        }
053        return PREPARED | READONLY;
054    }
055
056    public void commit(long id, Serializable context) {
057        sleep (commitDelay);
058    }
059
060    public void abort(long id, Serializable context) {
061        sleep (abortDelay);
062    }
063
064    public void setConfiguration(Configuration cfg) throws ConfigurationException {
065        prepareDelay = cfg.getLong ("prepare-delay");
066        commitDelay  = cfg.getLong ("commit-delay");
067        abortDelay   = cfg.getLong ("abort-delay");
068        random       = cfg.getBoolean ("random") ? new Random() : null;
069        delayName    = cfg.get("delay-name", "DELAY");
070    }
071    void sleep (long delay) {
072        if (delay > 0L) {
073            try {
074                Thread.sleep (random != null ? (long)(random.nextDouble()*delay) : delay);
075            } catch (InterruptedException ignored) { }
076        } else
077            Thread.yield();
078    }
079}