我有一個假設的場景:讓我們假裝我有一個Apache Camel websocket服務器,並且允許多個websocket連接。每個客戶端連接都需要與一個ClientID關聯。 ClientID是通過一個新的連接通過InitConnection json消息獲得的,其中ClientID是消息的成員。問題是:爲了執行基於內容的路由,是否有可能讓駱駝將一個帶有ClientID的websocket實例關聯起來?基於Apache Camel內容的Websocket連接路由
1
A
回答
2
是的,這是可能的。您可以通過以下方法來檢索每個客戶的UUID:
from("direct:Consumer1")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
Map<String, Object> headers=exchange.getIn().getHeaders();
//you can get a unique connection key from the exchange header.
//store this key somewhere, to send messages to particular client.
String uniqueConnectionKey=headers.get("websocket.connectionKey").toString();
//you can get message from the client like below.
String dataFromClient=exchange.getIn().getBody().toString();
}
}).end();
你需要地圖烏爾客戶端id的這個唯一鍵,好讓你可以使用這個UUID將消息發送給特定的客戶端。
CamelContext camelContext=new DefaultCamelContext();
ProducerTemplate template=camelContext.createProducerTemplate();
template.sendBodyAndHeader("direct:Producer1", {message}, "connectionKey", {connectionkey});
直接:Producer1:生產者端點名稱。
connectionkey:一個唯一的連接密鑰,您將從websocket消費者的交換頭獲得。
消息:給websocket端點的消息。
編輯: 這裏是生產者路線。
from("direct:Producer1").
//we will use this connectionKey for uniquely identifying each connection from the client.
setHeader(WebsocketConstants.CONNECTION_KEY, header("connectionKey")).
to("websocket://{host}:{port}/camel-websocket?sendToAll=false").end();
相關問題
- 1. Apache Camel路由
- 2. 基於Web的可定製編輯器Apache-camel spring xml路由
- 3. 基於標記值的路由消息 - Apache Camel
- 4. 沒有連接緩存的Apache Camel http路由
- 5. Apache Camel有條件路由
- 6. 如果發生錯誤並繼續路由,Apache Camel從路由斷開連接
- 7. 基於內容的WCF路由
- 8. 基於駱駝內容的路由
- 9. 基於Drupal路由的節點內容
- 10. ASP.Net Web API:基於內容的路由
- 11. 基於內容的WCF路由器
- 12. 滷麪/基於內容的路由
- 13. 問題與基於內容的路由
- 14. Apache Camel:路由中的多個事務
- 15. Apache Camel中的動態路由器URI
- 16. Apache Camel - 路由中的交易
- 17. Apache Camel路由作爲Windows服務
- 18. Apache Camel:向AMQP輸出路由消息
- 19. Apache Camel - 在路由上使用GSON JsonSerializer
- 20. 臨時執行Apache Camel路由
- 21. Apache Camel緩慢啓動路由
- 22. Spring Boot Apache Camel路由測試
- 23. 如何縮放Apache Camel路由?
- 24. Apache Camel條件路由不起作用
- 25. Apache Camel:路由啓動控制
- 26. Apache Camel是否支持嵌套路由?
- 27. apache camel seda queue的查看內容
- 28. Apache Camel:再次呼叫路由或動態路由
- 29. 使用Apache Camel連接到Sonic ESB
- 30. 無法連接到SFTP uding apache camel
你的意思是駱駝執行添加調用json消息包含clientid? –
不,我想讓駱駝從客戶信息中提取客戶ID。由clientId從recipientList查找websocket。 – Rizon
這應該用choice()和when()來完成。但是,應該工作得很好。 –