View Javadoc

1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.mina.example.imagine.step3.server;
21  
22  import java.lang.management.ManagementFactory;
23  import java.net.InetSocketAddress;
24  import java.util.concurrent.Executors;
25  
26  import javax.management.MBeanServer;
27  import javax.management.ObjectName;
28  
29  import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
30  import org.apache.mina.example.imagine.step1.codec.ImageCodecFactory;
31  import org.apache.mina.filter.codec.ProtocolCodecFilter;
32  import org.apache.mina.filter.executor.ExecutorFilter;
33  import org.apache.mina.integration.jmx.IoFilterMBean;
34  import org.apache.mina.integration.jmx.IoServiceMBean;
35  import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
36  
37  /**
38   * entry point for the server used in the tutorial on protocol codecs
39   *
40   * @author The Apache MINA Project (dev@mina.apache.org)
41   * @version $Rev$, $Date$
42   */
43  
44  public class ImageServer {
45      public static final int PORT = 33789;
46  
47      public static void main(String[] args) throws Exception {
48          
49          // create a JMX MBean Server server instance
50          MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
51          
52          // Create a class that handles sessions, incoming and outgoing data.  For
53          // this step, we will pass in the MBeanServer so that sessions can be 
54          // registered on the MBeanServer.
55          ImageServerIoHandler handler = new ImageServerIoHandler( mBeanServer );
56          
57          // This socket acceptor will handle incoming connections
58          NioSocketAcceptor acceptor = new NioSocketAcceptor();
59          
60          // create a JMX-aware bean that wraps a MINA IoService object.  In this
61          // case, a NioSocketAcceptor.  
62          IoServiceMBean acceptorMBean = new IoServiceMBean( acceptor );
63          
64          // create a JMX ObjectName.  This has to be in a specific format.  
65          ObjectName acceptorName = new ObjectName( acceptor.getClass().getPackage().getName() + 
66              ":type=acceptor,name=" + acceptor.getClass().getSimpleName());
67          
68          // register the bean on the MBeanServer.  Without this line, no JMX will happen for
69          // this acceptor.
70          mBeanServer.registerMBean( acceptorMBean, acceptorName );
71          
72          // add an IoFilter .  This class is responsible for converting the incoming and 
73          // outgoing raw data to ImageRequest and ImageResponse objects
74          ProtocolCodecFilter protocolFilter = new ProtocolCodecFilter(new ImageCodecFactory(false));
75          
76          // create a JMX-aware bean that wraps a MINA IoFilter object.  In this
77          // case, a ProtocolCodecFilter
78          IoFilterMBean protocolFilterMBean = new IoFilterMBean( protocolFilter );
79          
80          // create a JMX ObjectName.
81          ObjectName protocolFilterName = new ObjectName( protocolFilter.getClass().getPackage().getName() + 
82              ":type=protocolfilter,name=" + protocolFilter.getClass().getSimpleName() );
83          
84          // register the bean on the MBeanServer.  Without this line, no JMX will happen for
85          // this filter.
86          mBeanServer.registerMBean( protocolFilterMBean, protocolFilterName );
87          
88          // add the protocolFilter to the acceptor, otherwise no filtering of data will happen
89          acceptor.getFilterChain().addLast("protocol", protocolFilter);
90          
91          // get a reference to the filter chain from the acceptor
92          DefaultIoFilterChainBuilder filterChainBuilder = acceptor.getFilterChain();
93          
94          // add an ExecutorFilter to the filter chain.  The preferred order is to put the executor filter
95          // after any protocol filters due to the fact that protocol codecs are generally CPU-bound
96          // which is the same as I/O filters.
97          filterChainBuilder.addLast("threadPool", new ExecutorFilter(Executors.newCachedThreadPool()));
98          
99          // set this NioSocketAcceptor's handler to the ImageServerHandler
100         acceptor.setHandler(handler);
101         
102         // Bind to the specified address.  This kicks off the listening for 
103         // incoming connections
104         acceptor.bind(new InetSocketAddress(PORT));
105         System.out.println("Step 3 server is listenig at port " + PORT);
106     }
107 }