Skip to content

fix(chat): make SimpMessagingTemplate optional in ChatSessionService#19

Merged
mpge merged 1 commit intomainfrom
fix/optional-messaging-template
Apr 19, 2026
Merged

fix(chat): make SimpMessagingTemplate optional in ChatSessionService#19
mpge merged 1 commit intomainfrom
fix/optional-messaging-template

Conversation

@mpge
Copy link
Copy Markdown
Member

@mpge mpge commented Apr 19, 2026

Summary

ChatSessionService declared SimpMessagingTemplate as a required constructor argument. WebSocketConfig only exposes that bean when escalated.broadcasting.enabled = true, so the application failed to boot whenever broadcasting was off — the same failure that forced the docker demo (#14) to ship the env var sidestep.

The fix is the proper one called out in #17: inject Optional<SimpMessagingTemplate>, so Spring hands the service an empty Optional when the bean isn't published. All four convertAndSend call sites are routed through a private broadcast() helper that no-ops when the template is absent. Chat persistence (ticketService.create, repository saves, status transitions) keeps working; only the out-of-band STOMP fan-out is skipped.

Closes #17

Test plan

  • Existing 8 tests still pass under the Optional.of(messagingTemplate) constructor wiring
  • New test start_succeedsWhenMessagingTemplateIsAbsent constructs the service with Optional.empty() and asserts start(...) does not throw and still persists the session
  • Once merged, the docker demo will no longer need ESCALATED_BROADCASTING_ENABLED=true to boot — that env var sidestep can be removed in a follow-up to feat(demo): Docker dev env scaffold (DRAFT, not end-to-end) #14

Compatibility

This is a constructor signature change (SimpMessagingTemplateOptional<SimpMessagingTemplate>). Any consumer wiring the service manually (rather than via Spring's @Service autoinjection) needs to wrap their argument. No existing @Service-injected consumer in this repo or the demo host needs changes — Spring resolves Optional<X> automatically.

ChatSessionService failed startup whenever the broadcasting feature was
disabled because the constructor required a SimpMessagingTemplate bean,
but WebSocketConfig only provides one when escalated.broadcasting.enabled
is true. Spring resolved the missing bean as a fatal startup error rather
than degrading gracefully.

Switch the field to Optional<SimpMessagingTemplate> so Spring injects an
empty Optional when broadcasting is off. All four convertAndSend call
sites are routed through a private broadcast() helper that no-ops when
the template is absent, so chat persistence (DB writes) keeps working
while the STOMP fan-out is simply skipped.

Adds a unit test that constructs the service with Optional.empty() and
verifies start() succeeds without throwing.

Closes #17
@mpge mpge merged commit 565f82a into main Apr 19, 2026
2 checks passed
@mpge mpge deleted the fix/optional-messaging-template branch April 19, 2026 20:03
@mpge mpge mentioned this pull request Apr 24, 2026
1 task
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.

ChatSessionService unconditionally injects SimpMessagingTemplate which is conditionally provided

1 participant