javamqmsgque 9.0
Loading...
Searching...
No Matches
Example: Filter6

Documentation of the Filter6 tool used for trans2.test.

INTRODUCTION

The Filer6 tool is used to test the filter-feature of javamqmsgque.

To run the filter test, a first client, one or more filters and a final server are created. All are connected to the javamqmsgque protocol.

The trans2.test carries out common filter tests and special stress tests. A stress test is performed by exiting one or more filters or servers and observing the response and behavior when reconnecting.

The GOAL for this test is:

CODE filter

/**
 *   @file         example/java/Filter6.java
 *   @brief        Filter6.java - 25 Feb 2023 - aotto1968
 *   @copyright    (C) NHI - #1 - Project - Group
 *                 This software has NO permissions to copy,
 *                 please contact AUTHOR for additional information
 *   @version      dfcad032825adad5ec3d7683b557416346c66411
 *   @date         Sat Feb 25 13:55:56 2023 +0100
 *   @author       aotto1968 <aotto1968@t-online.de>
 */

package example;

import java.util.Queue;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javamsgque.mqmsgque.*;
import javamsgque.mkkernel.*;

class Filter6 extends MqContextC implements IServerSetup, IServerCleanup, IEvent {

  private Pattern rgx = null;
  private MkLogFileC FH = null;

  public Filter6(MqContextC tmpl) {
    super(tmpl);
    ConfigSetIgnoreExit(true);
    rgx = Pattern.compile("^(?:Filter6-1|Filter6|fs1.*)$");
  }

  private void ErrorWrite() {
    MkErrorC err = ErrorDEFAULT();
    try {
      FH.WriteC("ERROR: " + err.GetText());
    } catch (Throwable ex) {
      err.Catch(ex).Println();
    } finally {
      err.Reset();
    }
  }

  private class LOGF implements ICallback {
    public void Callback(MqContextC ctx) {
      MqContextC ftr = SlaveGetFilter();
      if (rgx.matcher(ftr.ConfigGetName()).matches()) {
        FH = MkLogFileC.Open(Filter6.this,ReadC());
      } else {
	ProxyForward(ftr);
      }
      SendRETURN();
    }
  }

  private class WRIT implements ICallback {
    public void Callback(MqContextC ctx) {
      var master = (Filter6) ctx.SlaveGetMaster();
      if (master.rgx.matcher(ctx.ConfigGetName()).matches()) {
        master.FH.WriteC(ctx.ReadC());
      } else {
        ctx.ProxyForward(master);
      }
      ctx.SendRETURN();
    }
  }

  private class EXIT implements ICallback {
    public void Callback(MqContextC ctx) {
      Exit();
    }
  }

  private class SOEX implements ICallback {
    public void Callback(MqContextC ctx) {
      ErrorDEFAULT().SetEXIT();
    }
  }

  private class FilterIn implements ICallback {
    public void Callback(MqContextC ctx) {
      StorageExport();
      SendRETURN();
    }
  }

  public void Event() {
    if (StorageCount() == 0L) {
      ErrorDEFAULT().SetCONTINUE();
    } else {
      int Id = 0;
      try {
        MqContextC ftr = SlaveGetFilter();
	Id = StorageImport();
        try {
          ProxyForward(ftr);
        } catch (MkExceptionC ex) {
          int errCnt = StorageErrCnt(Id);
          if (errCnt < 3) {
            StorageDecrRef(Id);
            return;
          } else {
            throw ex;
          }
        } catch (Throwable ex) {
          throw ex;
        }
      } catch (Throwable ex) {
        ErrorCatch (ex);
        ErrorWrite();
      }
      Id = StorageDelete(Id);
    }
  }

  public void ServerCleanup() {
    if (FH == null) return;
    FH.Dispose();
    FH = null;
  }

  // [filter_service_example]
  public void ServerSetup() {
    MqContextC ftr = SlaveGetFilter();
    ServiceCreate     ("LOGF", new LOGF()); 
    ServiceCreate     ("EXIT", new EXIT()); 
    ServiceCreate     ("SOEX", new SOEX());
    ServiceCreate     ("+ALL", new FilterIn()); 
    ServiceStorage    ("PRNT"); 
    ServiceStorage    ("PRN2"); 
    ftr.ServiceCreate ("WRIT", new WRIT());
    ServiceCreate     ("WRIT", new WRIT());
    ftr.ServiceProxy  ("WRT2", MqSlaveE.MASTER.get());
  }
  // [filter_service_example]

  /// [error_example]
  public static void main(String[] argv) {
    MqContextC srv = MqFactoryC.Add(Filter6.class).New();
    try {
      srv.LinkCreate(argv);
      srv.ProcessEvent(MqWaitOnEventE.FOREVER);
    } catch (Throwable e) {
      srv.ErrorCatch(e);
    } finally {
      srv.Exit();
    }
  }
  /// [error_example]
}