-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow creating NpgmqClient using a connection object, to allow for be…
…tter control over connection lifetime and to support transactions.
- Loading branch information
1 parent
9bcead5
commit 204a884
Showing
9 changed files
with
309 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,65 @@ | ||
using System.Reflection; | ||
using Microsoft.Extensions.Configuration; | ||
using Npgmq; | ||
using Npgsql; | ||
|
||
var configuration = new ConfigurationBuilder() | ||
.AddEnvironmentVariables() | ||
.AddUserSecrets(Assembly.GetExecutingAssembly()) | ||
.Build(); | ||
|
||
var npgmq = new NpgmqClient(configuration.GetConnectionString("ExampleDB")!); | ||
var connectionString = configuration.GetConnectionString("ExampleDB")!; | ||
|
||
await npgmq.InitAsync(); | ||
await npgmq.CreateQueueAsync("example_queue"); | ||
|
||
var msgId = await npgmq.SendAsync("example_queue", new MyMessageType | ||
// Test Npgmq with connection string | ||
{ | ||
Foo = "Test", | ||
Bar = 123 | ||
}); | ||
Console.WriteLine($"Sent message with id {msgId}"); | ||
var npgmq = new NpgmqClient(connectionString); | ||
|
||
await npgmq.InitAsync(); | ||
await npgmq.CreateQueueAsync("example_queue"); | ||
|
||
var msgId = await npgmq.SendAsync("example_queue", new MyMessageType | ||
{ | ||
Foo = "Connection string test", | ||
Bar = 1 | ||
}); | ||
Console.WriteLine($"Sent message with id {msgId}"); | ||
|
||
var msg = await npgmq.ReadAsync<MyMessageType>("example_queue"); | ||
if (msg != null) | ||
{ | ||
Console.WriteLine($"Read message with id {msg.MsgId}: Foo = {msg.Message?.Foo}, Bar = {msg.Message?.Bar}"); | ||
await npgmq.ArchiveAsync("example_queue", msg.MsgId); | ||
} | ||
} | ||
|
||
var msg = await npgmq.ReadAsync<MyMessageType>("example_queue"); | ||
if (msg != null) | ||
// Test Npgmq with connection object and a transaction | ||
{ | ||
Console.WriteLine($"Read message with id {msg.MsgId}: Foo = {msg.Message?.Foo}, Bar = {msg.Message?.Bar}"); | ||
await npgmq.ArchiveAsync("example_queue", msg.MsgId); | ||
await using var connection = new NpgsqlConnection(connectionString); | ||
await connection.OpenAsync(); | ||
var npgmq = new NpgmqClient(connection); | ||
|
||
await using (var tx = connection.BeginTransaction()) | ||
{ | ||
var msgId = await npgmq.SendAsync("example_queue", new MyMessageType | ||
{ | ||
Foo = "Connection object test", | ||
Bar = 2 | ||
}); | ||
Console.WriteLine($"Sent message with id {msgId}"); | ||
|
||
await tx.CommitAsync(); | ||
} | ||
|
||
var msg = await npgmq.ReadAsync<MyMessageType>("example_queue"); | ||
if (msg != null) | ||
{ | ||
Console.WriteLine($"Read message with id {msg.MsgId}: Foo = {msg.Message?.Foo}, Bar = {msg.Message?.Bar}"); | ||
await npgmq.ArchiveAsync("example_queue", msg.MsgId); | ||
} | ||
} | ||
|
||
internal class MyMessageType | ||
{ | ||
public string Foo { get; set; } = null!; | ||
public int Bar { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.