private void ProcessTasks(SqlSettings clientSettings)
        {
            var timer = Stopwatch.StartNew();

            var taskList = new List<Task>();
            for (int i = 0; i < 10; ++i)
            {
                int processId = i;
                var task = Task.Factory.StartNew(
                    () => TaskProcess(processId, clientSettings),
                    TaskCreationOptions.LongRunning);
                taskList.Add(task);
            }

            while (taskList.Count > 0)
            {
                for (int i = taskList.Count - 1; i >= 0; i--)
                {
                    var task = taskList[i];
                    if (task.IsCanceled)
                    {
                        Console.WriteLine("Task canceled!");
                        taskList.RemoveAt(i);
                    }
                    if (task.IsFaulted)
                    {
                        Console.WriteLine("Task faulted!");

                        taskList.RemoveAt(i);
                    }

                    if (task.IsCompleted)
                    {
                        taskList.RemoveAt(i);
                    }

                    if (Identities.Count > 0)
                    {
                        Guid identity;
                        if (Identities.TryDequeue(out identity))
                        {
                            var messengerClient = new Core.Services.Messenger(clientSettings, new MsSqlRepositoryFactory(clientSettings));
                            if (messengerClient.Cancel(identity))
                            {
                                Console.WriteLine("Canceled {0}", identity);
                            }
                        }
                    }
                }
            }

            timer.Stop();

            Console.WriteLine("All tasks finished. Time: " + timer.Elapsed);
        }
        private void ProcessTasks(string connectionString)
        {
            var clientSettings = new SqlSettings { ConnectionString = connectionString };
            var messengerClient = new Core.Services.Messenger(clientSettings, new MsSqlRepositoryFactory(clientSettings));

            for (int i = 0; i < 1; ++i)
            {
                int index = i+1;
/*
                messengerClient.Publish<FatQueueFailingEventHandler>(
                    handler => handler.Handle(new FatQueueFailingEvent {Fail = (index%9 == 0)}));
*/
                messengerClient.Publish<FatQueueFailingEventHandler>(
                    handler => handler.Handle(new FatQueueFailingEvent {Fail = true}));
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            var source = new CancellationTokenSource();

            try
            {
                StartServer(source, ConnectionString);

                var clientSettings = new SqlSettings
                {
                    ConnectionString = ConnectionString,
                    Logger = new ConsoleLogger(true)
                };

                //var executor = new LongRunningTestExecutor();
                //executor.Execute(clientSettings);

                //var failingExecutor = new FailingMessagesTestExecutor();
                //failingExecutor.Execute(ConnectionString);

                //var signalRExecutor = new SignalRScriptExecutor();
                //signalRExecutor.Execute(ConnectionString);

                var manualPublisherExecutor = new ManualPublisherExecutor();
                manualPublisherExecutor.Execute(clientSettings);
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc);
            }

            Console.WriteLine("Done. Press enter to exit..");
            Console.ReadLine();

            source.Cancel();

            Console.WriteLine("Shut down?");
            Console.ReadLine();
        }
        private void TaskProcess(int processId, SqlSettings clientSettings)
        {
            int queueCount = Queues.Length;
            for (int i = 0; i < 10000; ++i)
            {
                int messageId = i;
                var messengerClient = new Core.Services.Messenger(clientSettings, new MsSqlRepositoryFactory(clientSettings));

                var request = new FatQueuePrintMessageEvent
                {
                    Message = new CustomMessage {Message = $"[{processId.ToString().PadLeft(3, '0')}] - {messageId}"}
                };

                var identity = Guid.NewGuid();
                var publishSettings = new PublishSettings
                {
                    Identity = identity,
                    DelayExecutionInSeconds = i % 10 == 0 ? 30 : 0
                };
                Identities.Enqueue(identity);

                messengerClient.Publish<FatQueuePrintMessageEventHandler>(handler => handler.Handle(request), new QueueName(Queues[processId % queueCount]), publishSettings);
            }
        }
 public void Execute(SqlSettings clientSettings)
 {
     Task.Factory.StartNew(() => ProcessTasks(clientSettings));
 }
 public PostgreSqlMessenger(SqlSettings settings) : base(settings, new PostgreSqlRepositoryFactory(settings))
 {
 }
 public MsSqlMessengerService(SqlSettings settings, ILogger logger) : base(logger, new MsSqlRepositoryFactory(settings))
 {
 }
 public MsSqlRepositoryFactory(SqlSettings settings) : base(() => new MsSqlRepository(settings))
 {
 }
 public MsSqlMessenger(SqlSettings settings) : base(settings, new MsSqlRepositoryFactory(settings))
 {
 }
 public MsSqlRepository(SqlSettings settings)
 {
     if (settings == null) throw new ArgumentNullException(nameof(settings));
     _connectionFactory = () => new SqlConnection(settings.ConnectionString);
 }
        public void Execute(SqlSettings clientSettings)
        {
            var random = new Random();
            var finished = false;
            while (!finished)
            {
                var key = Console.ReadKey(true);
                if (key.Key == ConsoleKey.Escape)
                {
                    finished = true;
                }
                else
                {
                    var messengerClient = new Core.Services.Messenger(clientSettings, new MsSqlRepositoryFactory(clientSettings));
                    //var messengerClient = new Core.Services.Messenger(clientSettings, new PostgreSqlRepositoryFactory(clientSettings));

                    int delayExecution = 0;
                    Guid? identity = null;
                    int queueIndex;
                    if (key.KeyChar >= '0' && key.KeyChar <= '9')
                    {
                        queueIndex = (key.KeyChar-'0');
                        delayExecution = 30;
                        identity = Guid.NewGuid();
                        Identities.Enqueue(identity.Value);
                    }
                    else if (key.Key == ConsoleKey.Delete)
                    {
                        var identityToCancel = Identities.Dequeue();
                        var canceled = messengerClient.Cancel(identityToCancel);
                        if (canceled)
                        {
                            Console.WriteLine("Canceled: {0}", identityToCancel);
                        }
                        else
                        {
                            Console.WriteLine("Unable to cancel anymore: {0}", identityToCancel);
                        }
                        continue;
                    }
                    else
                    {
                        queueIndex = random.Next(0, Queues.Length);
                    }

                    var queueName = Queues[queueIndex];

                    var publishSettings = new PublishSettings
                    {
                        DelayExecutionInSeconds = delayExecution,
                        Identity = identity
                    };

                    var request = new FatQueuePrintMessageEvent
                    {
                        Message = new CustomMessage { Message = "Published from main manually. Delay: " + delayExecution }
                    };

                    messengerClient.Publish<FatQueuePrintMessageEventHandler>(handler => handler.Handle(request), new QueueName(queueName), publishSettings);

                    Console.WriteLine("Message published to queue {0}", queueName);
                }
            }
        }
 public PostgreSqlRepositoryFactory(SqlSettings settings) : base(() => new PostgreSqlRepository(settings))
 {
 }