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 <a href="http://mina.apache.org">Apache MINA Project</a> 41 */ 42 43 public class ImageServer { 44 public static final int PORT = 33789; 45 46 public static void main(String[] args) throws Exception { 47 48 // create a JMX MBean Server server instance 49 MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); 50 51 // Create a class that handles sessions, incoming and outgoing data. For 52 // this step, we will pass in the MBeanServer so that sessions can be 53 // registered on the MBeanServer. 54 ImageServerIoHandler handler = new ImageServerIoHandler( mBeanServer ); 55 56 // This socket acceptor will handle incoming connections 57 NioSocketAcceptor acceptor = new NioSocketAcceptor(); 58 59 // create a JMX-aware bean that wraps a MINA IoService object. In this 60 // case, a NioSocketAcceptor. 61 IoServiceMBean acceptorMBean = new IoServiceMBean( acceptor ); 62 63 // create a JMX ObjectName. This has to be in a specific format. 64 ObjectName acceptorName = new ObjectName( acceptor.getClass().getPackage().getName() + 65 ":type=acceptor,name=" + acceptor.getClass().getSimpleName()); 66 67 // register the bean on the MBeanServer. Without this line, no JMX will happen for 68 // this acceptor. 69 mBeanServer.registerMBean( acceptorMBean, acceptorName ); 70 71 // add an IoFilter . This class is responsible for converting the incoming and 72 // outgoing raw data to ImageRequest and ImageResponse objects 73 ProtocolCodecFilter protocolFilter = new ProtocolCodecFilter(new ImageCodecFactory(false)); 74 75 // create a JMX-aware bean that wraps a MINA IoFilter object. In this 76 // case, a ProtocolCodecFilter 77 IoFilterMBean protocolFilterMBean = new IoFilterMBean( protocolFilter ); 78 79 // create a JMX ObjectName. 80 ObjectName protocolFilterName = new ObjectName( protocolFilter.getClass().getPackage().getName() + 81 ":type=protocolfilter,name=" + protocolFilter.getClass().getSimpleName() ); 82 83 // register the bean on the MBeanServer. Without this line, no JMX will happen for 84 // this filter. 85 mBeanServer.registerMBean( protocolFilterMBean, protocolFilterName ); 86 87 // add the protocolFilter to the acceptor, otherwise no filtering of data will happen 88 acceptor.getFilterChain().addLast("protocol", protocolFilter); 89 90 // get a reference to the filter chain from the acceptor 91 DefaultIoFilterChainBuilder filterChainBuilder = acceptor.getFilterChain(); 92 93 // add an ExecutorFilter to the filter chain. The preferred order is to put the executor filter 94 // after any protocol filters due to the fact that protocol codecs are generally CPU-bound 95 // which is the same as I/O filters. 96 filterChainBuilder.addLast("threadPool", new ExecutorFilter(Executors.newCachedThreadPool())); 97 98 // set this NioSocketAcceptor's handler to the ImageServerHandler 99 acceptor.setHandler(handler); 100 101 // Bind to the specified address. This kicks off the listening for 102 // incoming connections 103 acceptor.bind(new InetSocketAddress(PORT)); 104 System.out.println("Step 3 server is listenig at port " + PORT); 105 } 106 }