IoT Events Jms Injector via apache camel - Java Message

Java examples for Message:JMS

Description

IoT Events Jms Injector via apache camel

Demo Code

/*/*from w  w  w.  ja  v a2 s  . co  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.beam.samples.iot.camel;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;

import javax.jms.ConnectionFactory;
import java.util.Random;
import java.util.logging.Logger;

public class IoTEventsJmsInjector {

    private final static Logger LOGGER = Logger.getLogger("name");

    public static void main(String args[]) throws Exception {
        CamelContext context = new DefaultCamelContext();

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                "tcp://localhost:61616");
        context.addComponent("jms",
                JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));

        final Processor processor = new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                Random rand = new Random();
                exchange.getIn().setBody("Event " + rand.nextInt(),
                        String.class);
            }
        };

        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("timer:fire?period=5000").process(processor).to(
                        "jms:queue:iot");
            }
        });

        context.start();

        while (true)
            ;
    }

}

Related Tutorials