Skip to content

v7: UseActiveMq with options from configuration, and headers over ActiveMQ - #121

Merged
christopherread merged 1 commit into
v7from
v7-activemq
Oct 25, 2026
Merged

christopherread merged 1 commit into
v7from
v7-activemq

Conversation

@christopherread

Copy link
Copy Markdown
Owner

Next v7 increment: UseActiveMq() for #84, following UseRabbitMq() (#112), UseNats() (#118) and UseKafka() (#119). It also carries message headers, including trace context, over ActiveMQ (#90). Targets v7.

API

builder.Services.AddObvs(obvs => obvs
    .AddService<IOrderMessage>("orders", service => service
        .UseActiveMq(builder.Configuration.GetSection("Obvs:Orders:ActiveMq"))   // or .UseActiveMq(o => o.BrokerUri = "activemq:tcp://localhost:61616")
        .SerializedWith(new JsonMessageSerializer(), new JsonMessageDeserializerFactory(typeof(JsonMessageDeserializer<>)))));
  • Options: ActiveMqOptions has BrokerUri (required), UserName and Password. Each service's settings are named options keyed by the service name: IOptionsMonitor<ActiveMqOptions>.Get("orders").
  • Startup checks: a missing broker URI or serializer fails at startup, inside the same ObvsConfigurationException as any other problem. The message names the configuration key to set (for example Obvs:Orders:ActiveMq:BrokerUri) or the method to call.
  • Topics only: every subscriber on a topic gets every message, so each bus receives every message. v6's option to send chosen message types through queues isn't exposed yet.
  • Client IDs include the bus's instance ID. On .NET Framework, v6 builds the client ID from the process, user and service. Two buses for the same service in one process would get the same ID, and the broker refuses a duplicate. On .NET 10 the client ID already includes a random GUID, and the tests only run there, so this change isn't covered by a test.

A transport bug this exposed and its fix

Deadlock when disposing the bus after receiving a message. It's the same bug as in RabbitMQ (#113) and Kafka (#119, #120).

  • Cause: messages reached subscribers on the session's dispatch thread. Code that ran after a message arrived, including the code after awaiting one, therefore ran on that thread. Disposing the bus there stops the connection, which waits for that same thread forever.
  • Effect: every broker test hung when disposing its host. A stack dump showed Connection.Stop() → SessionExecutor.Stop() waiting on the dispatch thread it was running on.
  • Fix: messages are still deserialized and acknowledged on the dispatch thread, then handed to subscribers on the task pool, in order. This also applies to v6's fluent setup.
  • Existing tests changed: five tests in TestMessageSource.cs asserted OnNext straight after raising a fake message, which assumed delivery on the listener's thread. They now wait up to 5 seconds for delivery and then make the same assertion. Their assertions about deserializing and acknowledging are unchanged, since those still happen on the listener's thread.

Headers over ActiveMQ

  • Sending: each Obvs header becomes a message property under its own name, so other tools can read traceparent too. Obvs's own type-name property is never overwritten.
  • Receiving: every text property except the type name becomes a header on the received message. Properties set through v6's property-provider option therefore show up as headers too.
  • Header names: the broker and client accept names that aren't Java identifiers, such as x-tenant.
  • Compatibility: v6 receivers ignore the extra properties, so v6 and v7 services can still exchange messages.

CI

  • Broker: adds an apache/activemq-classic:6.2.0 service container. 6.2.0 is the newest Classic image tag on Docker Hub; locally I used Homebrew's 6.3.2.
  • Tests: runs the ActiveMQ broker tests alongside the other transports.

Tests (written first)

  • Configuration (no broker): 5 tests in Obvs.ActiveMQ.Tests/Hosting/TestUseActiveMq.cs. They cover:
    • options from configuration, including credentials;
    • options from code;
    • a missing broker URI, in configuration and in code;
    • a missing serializer.
  • End to end (Explicit, real broker): 7 tests in TestActiveMqHosting.cs. They cover:
    • commands and events;
    • request and response;
    • every bus in a process receiving every message;
    • two hosts in one process;
    • disposing the bus from a message callback;
    • trace continuation;
    • headers the app sets.
  • Red first:
    • All 12 failed with NotImplementedException before implementation.
    • Once UseActiveMq worked, every broker test hung on the deadlock above.
    • After the fix, the header and trace tests still failed on a missing x-tenant header until headers were carried.
  • Results:
    • All 7 broker tests passed 3 runs in a row against a local ActiveMQ 6.3.2, taking about a second per run.
    • The 24 ActiveMQ unit tests passed 5 runs in a row.
    • A clean build of the solution has 0 warnings, and all 220 unit tests pass.

Not in this increment

  • UseNetMq() with header support, which is the last transport.
  • Exposing ActiveMQ queues and acknowledgement modes in ActiveMqOptions.

🤖 Generated with Claude Code

…iveMQ

Adds UseActiveMq(IConfiguration) and UseActiveMq(Action<ActiveMqOptions>) for
AddObvs, with startup checks for a missing broker URI or serializer. Messages
reach subscribers on the task pool, so disposing the bus from code that runs
after receiving a message no longer deadlocks the session's dispatch thread.
Message headers, including trace context, travel as message properties.
CI now runs the ActiveMQ broker tests against an ActiveMQ Classic container.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@christopherread christopherread added this to the v7 milestone Oct 25, 2026
@christopherread christopherread self-assigned this Oct 25, 2026
@christopherread
christopherread merged commit 772587d into v7 Oct 25, 2026
1 check passed
@christopherread
christopherread deleted the v7-activemq branch October 25, 2026 21:15
christopherread added a commit that referenced this pull request Oct 25, 2026
…thread (#122)

MessageSource passed each message to subscribers synchronously on the NMS
session's dispatch thread. Code that ran after awaiting a message could
then run on that thread, and deadlock it by disposing the bus: stopping
the connection waits for the dispatch thread, forever. NMS skips that
wait only for code in the dispatch's own execution context, which an
await continuation isn't. Messages are now handed to subscribers on the
task pool, in order.

TestServiceBus.ShouldNotDeadlockWhenCodeAfterAwaitingAMessageDisposesTheBus
hung before this change. Five TestMessageSource tests now wait for
delivery instead of expecting it on the listener's thread. CI runs the
ActiveMQ broker tests against apache/activemq-classic.

Backport of the fix in v7 PR #121.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
christopherread added a commit that referenced this pull request Oct 25, 2026
* Add v7 'AI native' design doc (#97)

* Add v7 'AI native' design doc

Proposes how v7 changes configuration, message registration, handlers,
testing, errors, observability, contracts, packaging and docs so AI
coding agents (and people) can use Obvs correctly first time. Each
change links to its tracking issue in the v7 milestone (#84-#95).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Design doc: frame the async APIs as layers over the observable core

Section 4 now explains why v7 stays observable-first: handlers, RequestAsync
and IAsyncEnumerable are thin adapters over IObservable, Rx stays first
class for stream composition, and analyzer OBVS006 catches Subscribe(async).
Resolves the open question on Rx: it stays in the core package.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Design doc: describe the bus lifetime under dependency injection

Section 1 now covers how the singleton bus is registered and owned, scoped
handlers, repeated AddObvs calls, keyed buses, and why one bus per
container needs a per-bus instance ID in broker-side names instead of the
process ID used by v6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* NetMQ: run each subscription's receive loop on its own thread (#110)

MessageSource started its receive loop with Task.Run. The loop blocks for
as long as the subscription lasts, so each subscription held a thread-pool
thread, and Subscribe waited until the loop got one. On CI's small runners,
the NetMQ tests start a dozen subscriptions at once; the pool added threads
slowly, subscribers took over 5 seconds to connect, publishers stopped
waiting for them, and the first messages were dropped. The loop now runs as
a LongRunning task on a dedicated thread.

TestMessageSourceThreading saturates the thread pool and checks that
subscribing doesn't wait for it. It failed before this change.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* RabbitMQ: deliver messages to subscribers off the consumer-dispatch thread (#113)

MessageSource passed each message to subscribers synchronously on
RabbitMQ.Client's consumer-dispatch thread. Code running after a message
arrived, including code after awaiting one (the continuation runs inline
there), could deadlock it: disposing the bus closes the connection, which
waits for that same thread. Messages are still deserialized on the
dispatch thread, while the body's buffer is valid, then handed to
subscribers on the task pool, in order.

TestServiceBus.ShouldNotDeadlockWhenASubscriberDisposesTheBus disposes the
bus from a message callback; it timed out before this change.

Backport of the fix in v7 PR #112.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* CI: pin the .NET SDK to 10.0.402 (#117)

Microsoft's download origin returns 409 for the new SDK 10.0.403 and
runtime 10.0.14, so setup-dotnet fails to install 10.0.x. Go back to
10.0.x once that release downloads.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* Kafka: deliver messages to subscribers off the consume thread (#120)

MessageSource called observer.OnNext on its consume thread, the task
started by Task.Run in Messages. Disposing the subscription cancels the
token and then waits for that task, so a subscriber that disposed the bus
or its subscription from a message callback waited on itself forever.
Messages are now handed to subscribers on the task pool, in order.

TestMessageSendAndReceive.ShouldNotDeadlockWhenASubscriberDisposesItsSubscription
disposes the subscription from a message callback; the message arrived but
the dispose timed out before this change.

Backport of the fix in v7 PR #119; the same fix for RabbitMQ was #113.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* ActiveMQ: deliver messages to subscribers off the session's dispatch thread (#122)

MessageSource passed each message to subscribers synchronously on the NMS
session's dispatch thread. Code that ran after awaiting a message could
then run on that thread, and deadlock it by disposing the bus: stopping
the connection waits for the dispatch thread, forever. NMS skips that
wait only for code in the dispatch's own execution context, which an
await continuation isn't. Messages are now handed to subscribers on the
task pool, in order.

TestServiceBus.ShouldNotDeadlockWhenCodeAfterAwaitingAMessageDisposesTheBus
hung before this change. Five TestMessageSource tests now wait for
delivery instead of expecting it on the listener's thread. CI runs the
ActiveMQ broker tests against apache/activemq-classic.

Backport of the fix in v7 PR #121.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant