1. 程式人生 > >Missian指南三:創建一個Missian服務器(使用spring)

Missian指南三:創建一個Missian服務器(使用spring)

hello executor accept Coding acceptor 3.1 aac builder pat

在使用Missian時,spring是可選的,但是作者本人強烈推薦和Spring配合使用。Spring是一個偉大的項目,並且它不會對程序在運行時的效率帶來任何損耗。

Missian在服務器端依賴與Mina,Missian只是提供一個Codec(協議編碼解碼,兼容TCP和HTTP)和一個Handler(調用Hessian序列化機制來反序列化數據、使用BeanLocator來定位這次調用的Bean)。熟悉Mina的朋友會很清楚Codec和Handler的概念;不熟悉的朋友也沒關系,按照這個教程一樣可以創建一個高效的服務來。對Mina沒有興趣的朋友可以直接跳到第七步:)

步驟一:創建一個spring配置文件。

Xml代碼 技術分享圖片
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  5. </beans>

步驟二:配置文件中加入:

Xml代碼 技術分享圖片
  1. <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  2. <property name="customEditors">
  3. <map>
  4. <entry key="java.net.SocketAddress">
  5. <bean class="org.apache.mina.integration.beans.InetSocketAddressEditor" />
  6. </entry>
  7. </map>
  8. </property>
  9. </bean>

這個是最後綁定端口時,用來將10.1.23.1:125轉換成SocketAddress的,不用太關註。

步驟三:配置各個Mina的Filter

註意ExecutorFitler是使用的默認構造函數,要指定線程數,或者將已有的線程池傳入,可以使用其它的構造函數;LoggingFilter中除了Exception之外的時間的Log級別已經全部設為DEBUG;CodecFilter是關鍵,這裏引入了Missian的編碼解碼器。

Java代碼 技術分享圖片
  1. <bean id="executorFilter" class="org.apache.mina.filter.executor.ExecutorFilter" />
  2. <bean id="codecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter">
  3. <constructor-arg>
  4. <bean class="com.missian.server.codec.MissianCodecFactory" />
  5. </constructor-arg>
  6. </bean>
  7. <bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter">
  8. <property name="messageReceivedLogLevel" value="DEBUG"/>
  9. <property name="messageSentLogLevel" value="DEBUG"/>
  10. <property name="sessionCreatedLogLevel" value="DEBUG"/>
  11. <property name="sessionClosedLogLevel" value="DEBUG"/>
  12. <property name="sessionIdleLogLevel" value="DEBUG"/>
  13. <property name="sessionOpenedLogLevel" value="DEBUG"/>
  14. </bean>

步驟四:構建FilterChian

這裏我把Codec放在線程池之前,因為編碼解碼是CPU密集型的操作,使用線程池並不能提高效率。當然了,有興趣的朋友可以自己調整順序做一下測試。

Java代碼 技術分享圖片
  1. <bean id="filterChainBuilder"
  2. class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
  3. <property name="filters">
  4. <map>
  5. <entry key="codecFilter" value-ref="codecFilter" />
  6. <entry key="executor" value-ref="executorFilter" />
  7. <entry key="loggingFilter" value-ref="loggingFilter" />
  8. </map>
  9. </property>
  10. </bean>

步驟五:創建IoHandler。

這一步也很重要,引入了Missian的處理器,就是在這裏調用了Hessian的序列化機制,並完成對相應的Bean的調用。

Xml代碼 技術分享圖片
  1. <bean id="minaHandler" class="com.missian.server.handler.MissianHandler">
  2. <constructor-arg>
  3. <bean class="com.missian.common.beanlocate.SpringLocator" />
  4. </constructor-arg>
  5. </bean>

MissianHandler接受一個BeanLocator的構造菜熟,註意這裏直接給MissianHandler註入了一個SpringLocator,使得Missian有能力去Spring去尋找相應的Bean。

這裏是一個很好的擴展點,有需要的話可以在BeanLocator上做做文章。

步驟六:創建一個Acceptor,監聽端口

Java代碼 技術分享圖片
  1. <bean id="minaAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor"
  2. init-method="bind" destroy-method="unbind">
  3. <property name="defaultLocalAddress" value=":1235" />
  4. <property name="handler" ref="minaHandler" />
  5. <property name="reuseAddress" value="true" />
  6. <property name="filterChainBuilder" ref="filterChainBuilder" />
  7. </bean>

到此位置,missian服務配置完畢。接下來配置一下業務邏輯的Bean。

步驟七:配置一個業務邏輯Bean,供Missian客戶端調用

Java代碼 技術分享圖片
  1. <bean id="hello" class="com.missian.example.bean.HelloImpl"></bean>

上一篇指南裏面創建的這個類第一次出境了,鼓掌……

註意bean的id叫做‘hello’,missian客戶端就是通過‘hello’這個名稱找到這個bean的,例如:http://www.abc.cn/hello。

值得一提的是如果客戶端想通過http://www.abc.cn/p/hello來訪問這個bean,那麽這個bean的配置應該如此:

Java代碼 技術分享圖片
  1. <bean name="p/hello" class="com.missian.example.bean.HelloImpl"></bean>

ID屬性是不能出現斜杠的,所以通過name來定義這個bean。

步驟八:啟動服務器

Java代碼 技術分享圖片
  1. public class ServerWithSpring {
  2. /**
  3. * @param args
  4. */
  5. public static void main(String[] args) {
  6. new ClassPathXmlApplicationContext("com/missian/example/server/withspring/applicationContext-*.xml");
  7. }
  8. }

運行ServerWithSpring即啟動了整個服務了。服務將監聽1235端口,接受HTTP協議和TCP協議格式的請求。

步驟九:用Hessian來調用此服務

由於Missian服務器是兼容Hessian的,所以,在創建Missian客戶端之前,讓我們用Hessian客戶端來測試一下這個服務吧。

Java代碼 技術分享圖片
  1. HessianProxyFactory factory = new HessianProxyFactory();
  2. Hello hello = (Hello) factory.create(Hello.class, "http://localhost:1235/hello");
  3. System.out.println(hello.hello("test", 27));

是的,你會發現調用成功了。

完整的配置文件如下

Java代碼 技術分享圖片
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  5. <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
  6. <property name="customEditors">
  7. <map>
  8. <entry key="java.net.SocketAddress">
  9. <bean class="org.apache.mina.integration.beans.InetSocketAddressEditor" />
  10. </entry>
  11. </map>
  12. </property>
  13. </bean>
  14. <!-- The IoHandler implementation -->
  15. <bean id="minaHandler" class="com.missian.server.handler.MissianHandler">
  16. <constructor-arg>
  17. <bean class="com.missian.common.beanlocate.SpringLocator" />
  18. </constructor-arg>
  19. </bean>
  20. <!-- the IoFilters -->
  21. <bean id="executorFilter" class="org.apache.mina.filter.executor.ExecutorFilter" />
  22. <bean id="codecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter">
  23. <constructor-arg>
  24. <bean class="com.missian.server.codec.MissianCodecFactory" />
  25. </constructor-arg>
  26. </bean>
  27. <bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter">
  28. <property name="messageReceivedLogLevel" value="DEBUG"/>
  29. <property name="messageSentLogLevel" value="DEBUG"/>
  30. <property name="sessionCreatedLogLevel" value="DEBUG"/>
  31. <property name="sessionClosedLogLevel" value="DEBUG"/>
  32. <property name="sessionIdleLogLevel" value="DEBUG"/>
  33. <property name="sessionOpenedLogLevel" value="DEBUG"/>
  34. </bean>
  35. <!-- The non-SSL filter chain. -->
  36. <bean id="filterChainBuilder"
  37. class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
  38. <property name="filters">
  39. <map>
  40. <entry key="codecFilter" value-ref="codecFilter" />
  41. <entry key="executor" value-ref="executorFilter" />
  42. <entry key="loggingFilter" value-ref="loggingFilter" />
  43. </map>
  44. </property>
  45. </bean>
  46. <!-- The IoAcceptor which binds to port 1235 server side -->
  47. <bean id="minaAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor"
  48. init-method="bind" destroy-method="unbind">
  49. <property name="defaultLocalAddress" value=":1235" />
  50. <property name="handler" ref="minaHandler" />
  51. <property name="reuseAddress" value="true" />
  52. <property name="filterChainBuilder" ref="filterChainBuilder" />
  53. </bean>
  54. <!-- your business bean, missian client will call this bean by ‘hello‘ -->
  55. <bean id="hello" class="com.missian.example.bean.HelloImpl"></bean>
  56. </beans>

Missian指南三:創建一個Missian服務器(使用spring)