Java tutorial
/* * Copyright 2002-2012 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 com.tcp.client; import java.io.IOException; import java.net.ServerSocket; import java.util.HashMap; import java.util.Map; import org.springframework.context.support.GenericXmlApplicationContext; import org.springframework.core.env.MapPropertySource; /** * Demonstrates the use of a gateway as an entry point into the integration flow. * The message generated by the gateway is sent over tcp by the outbound gateway * to the inbound gateway. In turn the inbound gateway sends the message to the * EchoService and the generated response is transmitted over tcp back to the server. */ public final class Main { /** * Prevent instantiation. */ private Main() { } /** * Load the Spring Integration Application Context * * @param args - command line arguments * @throws InterruptedException * @throws IOException */ public static void main(final String... args) throws InterruptedException, IOException { final GenericXmlApplicationContext context = Main.setupContext(); final SimpleGateway gateway = context.getBean(SimpleGateway.class); new EchoService().execute(gateway); } public static GenericXmlApplicationContext setupContext() throws InterruptedException, IOException { int availableServerSocket = 5682; final GenericXmlApplicationContext context = new GenericXmlApplicationContext(); System.out.println("===== Player 2 ====="); while (true) { try { ServerSocket s = new ServerSocket(availableServerSocket); s.close(); System.out.println("Player 1 is unavailable.. trying again!!"); Thread.sleep(2000); } catch (IOException e) { System.out.println("Found player 1!!"); break; } } final Map<String, Object> sockets = new HashMap<String, Object>(); sockets.put("availableServerSocket", availableServerSocket); final MapPropertySource propertySource = new MapPropertySource("sockets", sockets); context.getEnvironment().getPropertySources().addLast(propertySource); context.load("classpath:META-INF/context.xml"); context.registerShutdownHook(); context.refresh(); return context; } }