user1188867
user1188867

Reputation: 3978

Creating multiple instances of websocket message broker in Spring 4

I want to create multiple instances of message broker in Spring 4. I am using the following configuration:

<websocket:message-broker application-destination-prefix="/App1" >
    <websocket:stomp-endpoint path="/path1">
        <websocket:sockjs/>
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>

<websocket:message-broker  application-destination-prefix="/App2">
    <websocket:stomp-endpoint path="/path2">
        <websocket:sockjs/>
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic"/>
</websocket:message-broker>

And in my controller I am autowiring SimpMessagingTemplate.

I am getting the following exception:

Caused by: org.springframework.beans.factory.BeanCreationException:
Could not autowire field: org.springframework.messaging.simp.SimpMessagingTemplate
com.cdk.phoenix.app.controller.tools.tail.LogTailController.template; nested
exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [org.springframework.messaging.simp.SimpMessagingTemplate]
is defined: expected single matching bean but found 2:
org.springframework.messaging.simp.SimpMessagingTemplate#0,org.springframework.mes
saging.simp.SimpMessagingTemplate#1

How can I create unique instance of SimpMessagingTemplate for different message brokers?

Upvotes: 2

Views: 1520

Answers (1)

user1188867
user1188867

Reputation: 3978

As a temporary fix, I have added a qualifier as below:

@Autowired
@Qualifier("org.springframework.messaging.simp.SimpMessagingTemplate#1")
SimpMessagingTemplate template;

Everything is working as expected but one error message getting displayed in the logs :

*ERROR* SubProtocolWebSocketHandler: handleMessage - No session for [Payload byte[0]][Headers={simpMessageType=CONNECT_ACK, nativeHeaders={}, simpConnectMessage=[Payload byte[0]][Headers={simpMessageType=CONNECT, stompCommand=CONNECT, nativeHeaders={heart-beat=[10000,10000], accept-version=[1.1,1.0]}, simpSessionAttributes={}, simpSessionId=katcl9hg, id=2627fb55-4c8a-365c-7211-e543578e9ffb, timestamp=1438601578923}], simpSessionId=katcl9hg, id=4b4f7b0e-237a-7b3b-7d3e-247096e82b51, timestamp=1438601578924}]

Is there anyway to fix this error?

Thanks, Santosh

Upvotes: 1

Related Questions