Skip to content

Instantly share code, notes, and snippets.

View fatihdumanli's full-sized avatar
🎯
Focusing

Fatih fatihdumanli

🎯
Focusing
View GitHub Profile
@fatihdumanli
fatihdumanli / permute.go
Last active February 4, 2022 13:21
Finding all permutations of a string in Golang
package main
import (
"fmt"
"strings"
)
var result []string
func main() {
permuteString("", "golang")
@fatihdumanli
fatihdumanli / EventHandler.cs
Last active November 3, 2020 20:13
Example usage of IMessageHandler
using EventTower;
using MessagesCommon;
using System;
using System.Threading.Tasks;
namespace Worker
{
public class EventHandler
: IMessageHandler<MessagesCommon.CustomerEmailChanged>
{
@fatihdumanli
fatihdumanli / Program.cs
Last active November 3, 2020 20:16
Client sends command and publishing events
using EventTower;
using MessagesCommon;
namespace Sender
{
class Program
{
static void Main(string[] args)
{
//defining endpoint named sender
@fatihdumanli
fatihdumanli / IMessageHandler.cs
Last active November 3, 2020 20:15
IMessageHandler interface
using System.Threading.Tasks;
namespace EventTower
{
public interface IMessageHandler<T> where T: IMessage
{
Task Handle(T message);
}
}
@fatihdumanli
fatihdumanli / DefaultRabbitMQAdapter.cs
Created November 3, 2020 11:42
Setting the endpoint for receiving messages
public void StartConsuming()
{
consumerChannel = connection.CreateModel();
consumerChannel.ExchangeDeclare(COMMAND_EXCHANGE_NAME, ExchangeType.Direct);
consumerChannel.ExchangeDeclare(EVENT_EXCHANGE_NAME, ExchangeType.Fanout);
var queue = consumerChannel.QueueDeclare(queue: queueName);
consumerChannel.QueueBind(queue, COMMAND_EXCHANGE_NAME, routingKey: queueName);
consumerChannel.QueueBind(queue, EVENT_EXCHANGE_NAME, routingKey: queueName);
@fatihdumanli
fatihdumanli / DefaultRabbitMQAdapter.cs
Created November 3, 2020 11:40
Publishing an event
public void BasicPublish(IEvent @event)
{
using(var channel = connection.CreateModel())
{
channel.ExchangeDeclare(EVENT_EXCHANGE_NAME, type: ExchangeType.Fanout);
channel.BasicPublish(exchange: EVENT_EXCHANGE_NAME, routingKey: string.Empty, basicProperties: null, body: @event.ToJson().ToByteArray());
}
}
@fatihdumanli
fatihdumanli / DefaultRabbitMQAdapter.cs
Created November 3, 2020 11:34
Publishing a command to rabbitmq server
public void BasicPublish(ICommand command, string destination)
{
using (var channel = connection.CreateModel())
{
channel.ExchangeDeclare(COMMAND_EXCHANGE_NAME, type: ExchangeType.Direct);
channel.BasicPublish(exchange: COMMAND_EXCHANGE_NAME, routingKey: destination, basicProperties: null, body: command.ToJson().ToByteArray());
}
}
public void TryConnect()
{
var policy = RetryPolicy.Handle<SocketException>().Or<BrokerUnreachableException>()
.WaitAndRetry(retryCount, op => TimeSpan.FromSeconds(Math.Pow(2, op)), (ex, time) =>
{
Console.WriteLine("Couldn't connect to RabbitMQ server...");
});
policy.Execute(() =>
{
@fatihdumanli
fatihdumanli / MessageBusEndpoint.cs
Last active November 3, 2020 20:16
MessageBus Endpoint
using System;
using System.Linq;
using System.Reflection;
using MessageBus.Exceptions;
using Newtonsoft.Json;
using EventTower.Utils;
namespace EventTower
{
public class MessageBusEndpoint
@fatihdumanli
fatihdumanli / DefaultRabbitMQAdapter.cs
Last active November 3, 2020 20:16
RabbitMQAdapter implementation
using System;
using System.Net.Sockets;
using Newtonsoft.Json;
using Polly;
using Polly.Retry;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RabbitMQ.Client.Exceptions;
using EventTower.Extensions;