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 /** Default constructor; no instance state to initialise. */ 039 public Delay() {} 040 long prepareDelay = 0L; 041 long commitDelay = 0L; 042 long abortDelay = 0L; 043 Random random; 044 String delayName; 045 046 public int prepare(long id, Serializable context) { 047 long delay = prepareDelay; 048 if (context instanceof Context) { 049 Context ctx = (Context) context; 050 delay = Optional.ofNullable((Long) ctx.get(delayName)).orElse(delay); 051 ctx.checkPoint(" pre-delay " + delay); 052 sleep (delay); 053 ctx.checkPoint("post-delay " + delay); 054 } 055 return PREPARED | READONLY; 056 } 057 058 public void commit(long id, Serializable context) { 059 sleep (commitDelay); 060 } 061 062 public void abort(long id, Serializable context) { 063 sleep (abortDelay); 064 } 065 066 public void setConfiguration(Configuration cfg) throws ConfigurationException { 067 prepareDelay = cfg.getLong ("prepare-delay"); 068 commitDelay = cfg.getLong ("commit-delay"); 069 abortDelay = cfg.getLong ("abort-delay"); 070 random = cfg.getBoolean ("random") ? new Random() : null; 071 delayName = cfg.get("delay-name", "DELAY"); 072 } 073 void sleep (long delay) { 074 if (delay > 0L) { 075 try { 076 Thread.sleep (random != null ? (long)(random.nextDouble()*delay) : delay); 077 } catch (InterruptedException ignored) { } 078 } else 079 Thread.yield(); 080 } 081}