Theo
Theo

Reputation: 913

Spring, Camel, ActiveMQ, and WebSockets

I want to set up my browser similar to the example shown in Spring's example that I bet some of you are familiar with that uses websockets along with stomp to create an interactive webapp. It's here: https://spring.io/guides/gs/messaging-stomp-websocket/

However, instead of the input going directly through messaging and back to the client and outputting "Hello" + name on the page, I need it to go through a series of queues and applications routing it using camel. I can't quite figure out how to connect my system of queues with the browser.

Here is my camel Context currently that starts in a queue named "testQSource". I would like to have the input messages given to the browser by the client sent to testQSource using Stomp and then have it continue on it's way.

<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
    <from uri="jms:queue:testQSource"/>
    <to uri="myBean"/>
    <log message="Routing message from testQSource to testQDestination queue with data ${body}"/>
    <to uri="jms:queue:testQDestination"/>
    <to uri="finalBean"/>
    <log message="message: ${body}"/>
</route>
</camelContext>

<camel:camelContext id="camel-client">
    <camel:template id="camelTemplate" />
</camel:camelContext>

<bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="brokerURL" value="tcp://localhost:61616" />
</bean>

Here is my main class:

public class TestCamelSpring {

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("camelspring.xml");
    ProducerTemplate camelTemplate = context.getBean("camelTemplate", ProducerTemplate.class);

    System.out.println("Message Sending started");
    camelTemplate.sendBody("jms:queue:testQSource", "Sample Message");
    System.out.println("Message sent");

}

}

Can I just slip the SpringApplication.run into my main function and edit the camelContext or is there something more I need to do? I'm not sure how to send the client input to the testQSource.

I know it is a mouthful, but I've been struggling with this for a while and have hit a bit of a wall. Any and all help would be greatly appreciated!

Upvotes: 1

Views: 1102

Answers (1)

Willem Jiang
Willem Jiang

Reputation: 3291

If you don't want to write the any java code to send the message to message queue, you may consider to setup client camel context by using a timer to trigger the message sending, then you just need to start the Spring context.

<camel:camelContext id="camel-client">
    <camel:from uri="timer://foo?fixedRate=true&period=60000" />
    <camel:setBody>
       <simple>Simple Message!</simple>
    </camel:setBody>
    <camel:to uri="jms:queue:testQSource" /> 
</camel:camelContext>

Upvotes: 1

Related Questions