1 package org.kuali.ole.sip2.service;
2
3 import io.netty.bootstrap.ServerBootstrap;
4 import io.netty.channel.ChannelFuture;
5 import io.netty.channel.ChannelInitializer;
6 import io.netty.channel.ChannelOption;
7 import io.netty.channel.EventLoopGroup;
8 import io.netty.channel.nio.NioEventLoopGroup;
9 import io.netty.channel.socket.SocketChannel;
10 import io.netty.channel.socket.nio.NioServerSocketChannel;
11 import org.apache.log4j.Logger;
12 import org.kuali.ole.sip2.sip2Server.OLENettyServerHandler;
13
14
15
16
17 public class OLENettyServer implements Runnable {
18
19 protected int serverPort = 7052;
20 protected boolean isStopped = false;
21 protected Thread runningThread = null;
22 protected String serverUrl = null;
23 protected String circulationService = null;
24 private String message;
25 private static final Logger LOG = Logger.getLogger(OLENettyServer.class);
26 EventLoopGroup bossGroup = new NioEventLoopGroup();
27 EventLoopGroup workerGroup = new NioEventLoopGroup();
28 ChannelFuture f = null;
29
30 public OLENettyServer() {
31 }
32
33 public OLENettyServer(int port, String serverUrl, String circulationService) {
34 this.serverPort = port;
35 this.serverUrl = serverUrl;
36 this.circulationService = circulationService;
37 }
38
39 public void run() {
40 synchronized (this) {
41 this.runningThread = Thread.currentThread();
42 }
43 while (!isStopped()) {
44 try {
45 ServerBootstrap b = new ServerBootstrap();
46 b.group(bossGroup, workerGroup)
47 .channel(NioServerSocketChannel.class)
48 .childHandler(new ChannelInitializer<SocketChannel>() {
49 @Override
50 public void initChannel(SocketChannel ch)
51 throws Exception {
52
53 ch.pipeline().addLast(new OLENettyServerHandler(serverUrl, circulationService));
54 }
55 }).option(ChannelOption.SO_BACKLOG, 128)
56 .childOption(ChannelOption.SO_KEEPALIVE, true);
57 f = b.bind(serverPort).sync();
58 LOG.info("Server started and is ready for client connections....");
59 this.message = "Socket server started Successfully";
60 f.channel().closeFuture().sync();
61 } catch (Exception e) {
62 e.printStackTrace();
63 this.message = "Error starting server" + e;
64 }
65 }
66 }
67
68
69 public synchronized boolean isStopped() {
70 return this.isStopped;
71 }
72
73 public synchronized void stop() {
74 this.isStopped = true;
75 try {
76 workerGroup.shutdownGracefully();
77 bossGroup.shutdownGracefully();
78 } catch (Exception e) {
79 this.message = "Error closing server" + e;
80 throw new RuntimeException("Error closing server", e);
81 }
82 }
83
84 public String getMessage() {
85 return message;
86 }
87
88 public void setMessage(String message) {
89 this.message = message;
90 }
91 }