nhs.spring.integration.App.java Source code

Java tutorial

Introduction

Here is the source code for nhs.spring.integration.App.java

Source

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed 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 nhs.spring.integration;

import java.util.Date;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.PublishSubscribeChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.PollableChannel;
import org.springframework.integration.endpoint.PollingConsumer;
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.scheduling.support.PeriodicTrigger;

/**
 * Starts the Spring Context and will initialize the Spring Integration routes.
 *
 * @author NHS
 * @since 1.0
 */
public final class App {
    // polling message timeout set
    private final static int RECEIVE_TIMEOUT = 2000;

    public static void main(String... args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "spring-integration-context.xml");
        GameMessageHandler gameHandler = new GameMessageHandler();
        GameGenerator gameGenerator = context.getBean(GameGenerator.class);

        context.start();

        //MessageChannel input = context.getBean("input-channel", MessageChannel.class);
        //PollableChannel output = context.getBean("output-channel", PollableChannel.class);
        QueueChannel qChannel = context.getBean("game-channel", QueueChannel.class);

        PollingConsumer gameConsumer = new PollingConsumer(qChannel, gameHandler);
        gameConsumer.setReceiveTimeout(RECEIVE_TIMEOUT);
        gameConsumer.setBeanFactory(context);

        // Set up the poller using periodic trigger
        PeriodicTrigger periodicTrigger = new PeriodicTrigger(1000);
        periodicTrigger.setInitialDelay(5000);
        periodicTrigger.setFixedRate(false);

        PollerMetadata pollerMetadata = new PollerMetadata();
        pollerMetadata.setTrigger(periodicTrigger);
        pollerMetadata.setMaxMessagesPerPoll(3);

        gameConsumer.setPollerMetadata(pollerMetadata);

        // Starts the polling consumer in the other thread
        gameConsumer.start();

        Date today = new Date();

        // Generates messages and sends to the channel
        Game game = gameGenerator.generateGame("League of legend", "Riot Games", today, "Tom", "Michael", "AOS");

        qChannel.send(MessageBuilder.withPayload(game).build());

        /*
        PublishSubscribeChannel pubsubChannel = null;
            
        pubsubChannel.subscribe(gameHandler);
            
        input.send(MessageBuilder.withPayload("Spring Integration / Hello NHS").build());
        Message<?> reply = output.receive();
            
        System.out.println("Received :" + reply);
        */
    }
}