From 2f0ec5675771b17339605a800b7e74c04c5a2a51 Mon Sep 17 00:00:00 2001 From: bart-vmware <104792814+bart-vmware@users.noreply.github.com> Date: Tue, 13 Dec 2022 14:46:41 +0100 Subject: [PATCH] Update RabbitMQ sample --- .../Controllers/RabbitMQController.cs | 26 +++++++------- Connectors/src/RabbitMQ/README.md | 35 +++++++++++++------ Connectors/src/RabbitMQ/manifest-windows.yml | 1 + Connectors/src/RabbitMQ/manifest.yml | 1 + 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/Connectors/src/RabbitMQ/Controllers/RabbitMQController.cs b/Connectors/src/RabbitMQ/Controllers/RabbitMQController.cs index 97cdb2d7..ed85e963 100644 --- a/Connectors/src/RabbitMQ/Controllers/RabbitMQController.cs +++ b/Connectors/src/RabbitMQ/Controllers/RabbitMQController.cs @@ -19,12 +19,11 @@ namespace RabbitMQ.Controllers opt.Version = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12; // Only needed if want to disable certificate validations - opt.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateChainErrors | + opt.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateChainErrors | SslPolicyErrors.RemoteCertificateNameMismatch | SslPolicyErrors.RemoteCertificateNotAvailable; } } - public IActionResult Receive() { using (var connection = _rabbitConnection.CreateConnection()) @@ -32,7 +31,8 @@ namespace RabbitMQ.Controllers { CreateQueue(channel); var data = channel.BasicGet("rabbit-test", true); - if (data != null) { + if (data != null) + { ViewData["message"] = Encoding.UTF8.GetString(data.Body.ToArray()); } } @@ -42,28 +42,30 @@ namespace RabbitMQ.Controllers public IActionResult Send(string message) { - if (message != null && message != "") { + if (!string.IsNullOrEmpty(message)) + { using (var connection = _rabbitConnection.CreateConnection()) using (var channel = connection.CreateModel()) { CreateQueue(channel); var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "", - routingKey: "rabbit-test", - basicProperties: null, - body: body); + routingKey: "rabbit-test", + basicProperties: null, + body: body); } } + return View(); } protected void CreateQueue(IModel channel) { channel.QueueDeclare(queue: "rabbit-test", - durable: false, - exclusive: false, - autoDelete: false, - arguments: null); + durable: false, + exclusive: false, + autoDelete: false, + arguments: null); } } -} +} \ No newline at end of file diff --git a/Connectors/src/RabbitMQ/README.md b/Connectors/src/RabbitMQ/README.md index ee7913b9..82bf8338 100644 --- a/Connectors/src/RabbitMQ/README.md +++ b/Connectors/src/RabbitMQ/README.md @@ -4,29 +4,42 @@ ASP.NET Core sample app illustrating how to use [Steeltoe RabbitMQ Connector](https://docs.steeltoe.io/api/v3/connectors/rabbitmq.html) for connecting to a RabbitMQ service on CloudFoundry. This specific sample illustrates how to use a `RabbitMQ.Client` to send and receive messages on the bound RabbitMQ service. -## Pre-requisites - CloudFoundry +## General Pre-requisites + +1. Installed .NET Core SDK + +## Running Locally + +1. Installed RabbitMQ Server +1. Set [ASPNETCORE_ENVIRONMENT=Development] () + +## Running on CloudFoundry 1. Installed Pivotal CloudFoundry 1.7+ 1. (Optional) installed Windows support 1. Installed RabbitMQ CloudFoundry service -1. Installed .NET Core SDK ## Create RabbitMQ Service Instance on CloudFoundry -You must first create an instance of the RabbitMQ service in a org/space. +You must first create an instance of the RabbitMQ service in an org/space. 1. `cf target -o myorg -s development` -1. `cf create-service p-rabbitmq standard myRabbitMQService` +1. `cf create-service p.rabbitmq single-node myRabbitMQService` ## Publish App & Push to CloudFoundry 1. `cf target -o myorg -s development` 1. `cd samples/Connectors/src/RabbitMQ` -1. `dotnet restore --configfile nuget.config` -1. Publish app to a local directory, specifying the framework and runtime (select ONE of these commands): - * `dotnet publish -f netcoreapp3.1 -r linux-x64` -1. Push the app using the appropriate manifest (select ONE of these commands): - * `cf push -f manifest.yml -p bin/Debug/netcoreapp3.1/linux-x64/publish` +1. Push the app + - When using Windows containers: + - Publish app to a local directory, specifying the runtime: + * `dotnet restore --configfile nuget.config` + * `dotnet publish -r win-x64` + - Push the app using the appropriate manifest: + * `cf push -f manifest-windows.yml -p bin/Debug/net6.0/win-x64/publish` + - Otherwise: + - Push the app using the appropriate manifest: + * `cf push -f manifest.yml` > Note: The provided manifest will create an app named `rabbitmq-connector` and attempt to bind the app to RabbitMQ service `myRabbitMQService`. @@ -36,7 +49,7 @@ To see the logs as you startup and use the app: `cf logs rabbitmq-connector` On a Linux cell, you should see something like this during startup: -```bash +```text 2016-08-24T12:22:42.68-0400 [CELL/0] OUT Creating container 2016-08-24T12:22:43.04-0400 [STG/0] OUT Successfully destroyed container 2016-08-24T12:22:43.95-0400 [CELL/0] OUT Successfully created container @@ -62,4 +75,4 @@ To receive a RabbitMQ message that you have sent: click "Receive" in the menu, a --- -### See the Official [Steeltoe Service Connectors Documentation](https://steeltoe.io/docs/steeltoe-service-connectors) for a more in-depth walkthrough of the samples and more detailed information +### See the Official [Steeltoe Service Connectors Documentation](https://steeltoe.io/docs/steeltoe-service-connectors) for a more in-depth walkthrough of the samples and more detailed information. diff --git a/Connectors/src/RabbitMQ/manifest-windows.yml b/Connectors/src/RabbitMQ/manifest-windows.yml index 34336bad..d2d94614 100644 --- a/Connectors/src/RabbitMQ/manifest-windows.yml +++ b/Connectors/src/RabbitMQ/manifest-windows.yml @@ -1,6 +1,7 @@ --- applications: - name: rabbitmq-connector + random-route: true memory: 256M stack: windows buildpacks: diff --git a/Connectors/src/RabbitMQ/manifest.yml b/Connectors/src/RabbitMQ/manifest.yml index bb49a472..4fbd5dd5 100644 --- a/Connectors/src/RabbitMQ/manifest.yml +++ b/Connectors/src/RabbitMQ/manifest.yml @@ -1,6 +1,7 @@ --- applications: - name: rabbitmq-connector + random-route: true buildpacks: - dotnet_core_buildpack memory: 128M