java远程异步RPC框架 Missian(转)

Create server with spring.

Introduction

Create missian server with spring

Details

Step 1:Create an spring context configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>

Step 2:Create a custom editors to convert a 'host:port' string to SocketAddress

        <beanclass="org.springframework.beans.factory.config.CustomEditorConfigurer">
                <propertyname="customEditors">
                        <map>
                                <entrykey="java.net.SocketAddress">
                                        <beanclass="org.apache.mina.integration.beans.InetSocketAddressEditor"/>
                                </entry>
                        </map>
                </property>
        </bean>

Step 3:Build filters especially the Codec filter

        <beanid="executorFilter"class="org.apache.mina.filter.executor.ExecutorFilter"/>
        <beanid="codecFilter"class="org.apache.mina.filter.codec.ProtocolCodecFilter">
                <constructor-arg>
                        <beanclass="com.missian.server.codec.MissianCodecFactory"/>
                </constructor-arg>
        </bean>
        <beanid="loggingFilter"class="org.apache.mina.filter.logging.LoggingFilter">
                <propertyname="messageReceivedLogLevel"value="DEBUG"/>
                <propertyname="messageSentLogLevel"value="DEBUG"/>
                <propertyname="sessionCreatedLogLevel"value="DEBUG"/>
                <propertyname="sessionClosedLogLevel"value="DEBUG"/>
                <propertyname="sessionIdleLogLevel"value="DEBUG"/>
                <propertyname="sessionOpenLogLevel"value="DEBUG"/>
        </bean>

Step 4:Create the filter chain

        <beanid="filterChainBuilder"
                class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
                <propertyname="filters">
                        <map>
                                <entrykey="codecFilter"value-ref="codecFilter"/>
                                <entrykey="executor"value-ref="executorFilter"/>
                                <entrykey="loggingFilter"value-ref="loggingFilter"/>
                        </map>
                </property>
        </bean>

Step 5:Create the message handler

        <beanid="minaHandler"class="com.missian.server.handler.MissianHandler">
                <constructor-arg>
                        <beanclass="com.missian.common.beanlocate.SpringLocator"/>
                </constructor-arg>
        </bean>

Please node that we create an SpringLocator instance and inject it to the MissianHandler, so that the handler can lookup beans in the spring context.

SpringLocator is provided by missian and its source code as below:

publicclassSpringLocatorimplementsBeanLocator,ApplicationContextAware{
        privateApplicationContext applicationContext;
        @Override
        publicObject lookup(String beanName){
                return applicationContext.getBean(beanName);
        }

        @Override
        publicvoid setApplicationContext(ApplicationContext applicationContext)
                        throwsBeansException{
                this.applicationContext = applicationContext;
        }

}

Step 6: Mina NioSocketAcceptor

        <beanid="minaAcceptor"class="org.apache.mina.transport.socket.nio.NioSocketAcceptor"
                init-method="bind"destroy-method="unbind">
                <propertyname="defaultLocalAddress"value=":1235"/><!---->
                <propertyname="handler"ref="minaHandler"/>
                <propertyname="reuseAddress"value="true"/>
                <propertyname="filterChainBuilder"ref="filterChainBuilder"/>
        </bean>

Step 7:Create business beans

Create an interface, for example:

publicinterfaceHello{
        publicString hello(String name,int age);
}

Implements the business interface

publicclassHelloImplimplementsHello{

        @Override
        publicString hello(String name,int age){
                return"hi, "+name+", "+age;
        }

}

And now configure this business implementation in spring:

<beanid="hello"class="com.missian.example.bean.HelloImpl"></bean>

Step 8:Startup the server

        publicstaticvoid main(String[] args){
                newClassPathXmlApplicationContext("com/missian/example/server/withspring/applicationContext-*.xml");
        }

猜你喜欢

转载自songzhan.iteye.com/blog/1905556