DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Hello I'm yunus emre.I'm a 24 year old.I have been living in istanbul,Turkey.I'm incredibly passionate about what I do and enjoy working with other open minded, enthusiastic people. I also like to share my experiences with others and blog regularly here. Buzz Words : .NET 2.0 and upwards C# ASP.NET (Web Forms, MVC, Web API) HTML/CSS MSSQL RavenDB Various Data Access libraries - from Disconnected and connected for SqlClient, NHibernate and Entity Framework I currently have been working as a software engineer in intertech.I am developing software to functional requirements and fixing bugs on banking platform using microsoft technologies in here. I'm interested in backend software development and enterprise software organizations.I'm keen on mostly about software architectures , design patterns , object oriented programming and enterprise software development.I have wrote articles in my own blog and communities about these topics.Also i like drawing software diagrams To become a good software engineer requires a good theoretical background as well as practical experiences.Therefore I will try to improve myself daily.New technologies and experiences raises new ideas and so we can change the world with these ideas.I believe that. I want to develop software on international projects in a international atmosphere Yunus Emre has posted 10 posts at DZone. You can read more from them at their website. View Full User Profile

Processor class for critical jobs

01.08.2014
| 1344 views |
  • submit to reddit
public interface IProcessor
{
    void Process();
}
 
public abstract class BaseProcessor
    : IProcessor
{
    public delegate void ProcessorStartingEventHandler(params Object[] param);
    public delegate void ProcessorStartedEventHandler(params Object[] param);
    public delegate void ProcessorCompletedEventHandler(params Object[] param);
    public delegate void ProcessorErrorEventHandler(params Object[] param);
 
    public ProcessorStartingEventHandler ProcessorStarting = null;
    public ProcessorStartedEventHandler ProcessorStarted = null;
    public ProcessorCompletedEventHandler ProcessorCompleted = null;
    public ProcessorErrorEventHandler ProcessorError = null;
 
    // you can set status in the processor class 
    public ProcessorStatus.Current Status { get; private set; }
 
    // parameter variables
    public Parameters _parameters = null;
    public Parameters Parameters
    {
        set { _parameters = value; }
    }
 
    // threading mechanism (Semaphore)
    private static Semaphore _semaphore = null;
    private static object _semaphoreLockObj = new object();
        
    // singleton pattern for semaphore
    private void InitializeSemaphore(int jobCountLimit)
    {
        if (_semaphore == null)
        {
            lock (_semaphoreLockObj)
            {
                if (_semaphore == null)
                {
                    _semaphore = new Semaphore(jobCountLimit, jobCountLimit);
                }
            }
        }
    }
 
    // ctor methods
    public BaseProcessor(Parameters param, int jobCountLimit)
    {
        this._parameters = param;
        InitializeSemaphore(jobCountLimit);
    }
 
    public BaseProcessor(Parameters param)
        : this(param, 3)
    { }
 
    // Real Job Method 
    protected abstract void RunJob(object param, out object[] output);
 
    public void Process()
    {
        // starting loging for real job's log table
        this.Status = ProcessorStatus.Current.Starting;
 
        if (ProcessorStarting != null)
            ProcessorStarting(this.GetType());
 
        ThreadPool.QueueUserWorkItem(new WaitCallback(Job), this._parameters);
    }
 
    private void Job(object param)
    {
        object[] output = null;
 
        // started loging
        this.Status = ProcessorStatus.Current.Started;
        if (ProcessorStarted != null)
            ProcessorStarted(this.GetType());
 
        try
        {
            _semaphore.WaitOne();
            RunJob(param, out output);
        }
        catch (Exception ex)
        {
            this.Status = ProcessorStatus.Current.Error;
            if (ProcessorError != null)
            {
                ProcessorError(this.GetType(), ex);
            }
            // error logging
        }
        finally
        {
            _semaphore.Release();
        }
 
        // completing loging
        this.Status = ProcessorStatus.Current.Completing;
 
        if (ProcessorCompleted != null)
        {
            ProcessorCompleted(this.GetType(), output);
        }
 
        // completed loging
        this.Status = ProcessorStatus.Current.Completed;
    }
}
 
// Customized Processor Class for custom jobs
public class MainProcessor
    : BaseProcessor
{
    public MainProcessor(Parameters param, int jobLimit)
        : base(param, jobLimit)
    { }
 
    public MainProcessor(Parameters param)
        : base(param)
    { }
 
    protected override void RunJob(object param, out object[] output)
    {
        MainJob mj = new MainJob();
        output = mj.Start(param as Parameters);
    }
}
 
public class ProcessorStatus
{
    public enum Current
    {
        Starting = 1,
        Started = 2,
        Completing = 3,
        Completed = 4,
        Error = 5
    }
}
 
public class Parameters
{
    public IDictionary<string, Object> _prms = null;
 
    public Parameters()
    {
        this._prms = new Dictionary<string, Object>();
    }
 
 
    public void Add(string key, object value)
    {
        this.Remove(key);
 
        if (!String.IsNullOrEmpty(key))
            this._prms.Add(key, value);
    }
 
    public void Remove(string key)
    {
        if (this._prms.ContainsKey(key) && !String.IsNullOrEmpty(key))
            this._prms.Remove(key);
    }
 
    public object TryGetValue(string key)
    {
        object obj;
        _prms.TryGetValue(key, out obj);
        return obj;
    }
 
    public void Clear()
    {
        this._prms.Clear();
    }
}

public interface IJob
{
    object[] Start(Parameters paramList = null);
}
 
public abstract class BaseJob
    : IJob
{
    public abstract object[] Start(Parameters paramList = null);
}
 
public class MainJob
    : BaseJob
{
    public override object[] Start(Parameters paramList = null)
    {
        try
        {
            // fact job operations
            Console.WriteLine("Real Operational Job...");
        }
        catch (Exception ex)
        {
            return new object[] { ex.Message };
        }
        return null;
    }
}

There are a critical operation in large-scale enterprise projects,for instance accountingin the banking solutions ,payment operations in e-commerce projects etc.We code critical classes for such operations.We have to think all possibilities and cases in these type classes in other words We have to watch and logging the process(accounting,payment)  in any event.Here’s in the below codes I coding similar to critical operation jobs.


Gist links ;

https://gist.github.com/yemrekeskin/5841482

https://gist.github.com/yemrekeskin/5841488