001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.example.imagine.step3.server; 021 022import java.lang.management.ManagementFactory; 023import java.net.InetSocketAddress; 024import java.util.concurrent.Executors; 025 026import javax.management.MBeanServer; 027import javax.management.ObjectName; 028 029import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder; 030import org.apache.mina.example.imagine.step1.codec.ImageCodecFactory; 031import org.apache.mina.filter.codec.ProtocolCodecFilter; 032import org.apache.mina.filter.executor.ExecutorFilter; 033import org.apache.mina.integration.jmx.IoFilterMBean; 034import org.apache.mina.integration.jmx.IoServiceMBean; 035import org.apache.mina.transport.socket.nio.NioSocketAcceptor; 036 037/** 038 * entry point for the server used in the tutorial on protocol codecs 039 * 040 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 041 */ 042 043public class ImageServer { 044 public static final int PORT = 33789; 045 046 public static void main(String[] args) throws Exception { 047 048 // create a JMX MBean Server server instance 049 MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); 050 051 // Create a class that handles sessions, incoming and outgoing data. For 052 // this step, we will pass in the MBeanServer so that sessions can be 053 // registered on the MBeanServer. 054 ImageServerIoHandler handler = new ImageServerIoHandler( mBeanServer ); 055 056 // This socket acceptor will handle incoming connections 057 NioSocketAcceptor acceptor = new NioSocketAcceptor(); 058 059 // create a JMX-aware bean that wraps a MINA IoService object. In this 060 // case, a NioSocketAcceptor. 061 IoServiceMBean acceptorMBean = new IoServiceMBean( acceptor ); 062 063 // create a JMX ObjectName. This has to be in a specific format. 064 ObjectName acceptorName = new ObjectName( acceptor.getClass().getPackage().getName() + 065 ":type=acceptor,name=" + acceptor.getClass().getSimpleName()); 066 067 // register the bean on the MBeanServer. Without this line, no JMX will happen for 068 // this acceptor. 069 mBeanServer.registerMBean( acceptorMBean, acceptorName ); 070 071 // add an IoFilter . This class is responsible for converting the incoming and 072 // outgoing raw data to ImageRequest and ImageResponse objects 073 ProtocolCodecFilter protocolFilter = new ProtocolCodecFilter(new ImageCodecFactory(false)); 074 075 // create a JMX-aware bean that wraps a MINA IoFilter object. In this 076 // case, a ProtocolCodecFilter 077 IoFilterMBean protocolFilterMBean = new IoFilterMBean( protocolFilter ); 078 079 // create a JMX ObjectName. 080 ObjectName protocolFilterName = new ObjectName( protocolFilter.getClass().getPackage().getName() + 081 ":type=protocolfilter,name=" + protocolFilter.getClass().getSimpleName() ); 082 083 // register the bean on the MBeanServer. Without this line, no JMX will happen for 084 // this filter. 085 mBeanServer.registerMBean( protocolFilterMBean, protocolFilterName ); 086 087 // add the protocolFilter to the acceptor, otherwise no filtering of data will happen 088 acceptor.getFilterChain().addLast("protocol", protocolFilter); 089 090 // get a reference to the filter chain from the acceptor 091 DefaultIoFilterChainBuilder filterChainBuilder = acceptor.getFilterChain(); 092 093 // add an ExecutorFilter to the filter chain. The preferred order is to put the executor filter 094 // after any protocol filters due to the fact that protocol codecs are generally CPU-bound 095 // which is the same as I/O filters. 096 filterChainBuilder.addLast("threadPool", new ExecutorFilter(Executors.newCachedThreadPool())); 097 098 // set this NioSocketAcceptor's handler to the ImageServerHandler 099 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}