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");
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值