`
gh_aiyz
  • 浏览: 40034 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Missian指南三:创建一个Missian服务器(使用spring)

阅读更多

在使用Missian时,spring是可选的,但是作者本人强烈推荐和Spring配合使用。Spring是一个伟大的项目,并且它不会对程序在运行时的效率带来任何损耗。

 

Missian在服务器端依赖与Mina,Missian只是提供一个Codec(协议编码解码,兼容TCP和HTTP)和一个Handler(调用Hessian序列化机制来反序列化数据、使用BeanLocator来定位这次调用的Bean)。熟悉Mina的朋友会很清楚Codec和Handler的概念;不熟悉的朋友也没关系,按照这个教程一样可以创建一个高效的服务来。对Mina没有兴趣的朋友可以直接跳到第七步:)

 

步骤一:创建一个spring配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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>

 

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

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

这个是最后绑定端口时,用来将10.1.23.1:125转换成SocketAddress的,不用太关注。

 

步骤三:配置各个Mina的Filter

注意ExecutorFitler是使用的默认构造函数,要指定线程数,或者将已有的线程池传入,可以使用其它的构造函数;LoggingFilter中除了Exception之外的时间的Log级别已经全部设为DEBUG;CodecFilter是关键,这里引入了Missian的编码解码器。

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

 

步骤四:构建FilterChian

这里我把Codec放在线程池之前,因为编码解码是CPU密集型的操作,使用线程池并不能提高效率。当然了,有兴趣的朋友可以自己调整顺序做一下测试。

	<bean id="filterChainBuilder"
		class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
		<property name="filters">
			<map>
				<entry key="codecFilter" value-ref="codecFilter" />
				<entry key="executor" value-ref="executorFilter" />
				<entry key="loggingFilter" value-ref="loggingFilter" />
			</map>
		</property>
	</bean>
 

 

步骤五:创建IoHandler。

这一步也很重要,引入了Missian的处理器,就是在这里调用了Hessian的序列化机制,并完成对相应的Bean的调用。

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

MissianHandler接受一个BeanLocator的构造菜熟,注意这里直接给MissianHandler注入了一个SpringLocator,使得Missian有能力去Spring去寻找相应的Bean。

这里是一个很好的扩展点,有需要的话可以在BeanLocator上做做文章。

 

 

步骤六:创建一个Acceptor,监听端口

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

到此位置,missian服务配置完毕。接下来配置一下业务逻辑的Bean。

 

步骤七:配置一个业务逻辑Bean,供Missian客户端调用

<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的配置应该如此:

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

ID属性是不能出现斜杠的,所以通过name来定义这个bean。

 

 

步骤八:启动服务器

public class ServerWithSpring {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new ClassPathXmlApplicationContext("com/missian/example/server/withspring/applicationContext-*.xml");
	}
}

 运行ServerWithSpring即启动了整个服务了。服务将监听1235端口,接受HTTP协议和TCP协议格式的请求。

 

 

步骤九:用Hessian来调用此服务

由于Missian服务器是兼容Hessian的,所以,在创建Missian客户端之前,让我们用Hessian客户端来测试一下这个服务吧。

HessianProxyFactory factory = new HessianProxyFactory();

Hello hello = (Hello) factory.create(Hello.class, "http://localhost:1235/hello");
System.out.println(hello.hello("test", 27));

是的,你会发现调用成功了。

 

完整的配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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">

	<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
		<property name="customEditors">
			<map>
				<entry key="java.net.SocketAddress">
					<bean class="org.apache.mina.integration.beans.InetSocketAddressEditor" />
				</entry>
			</map>
		</property>
	</bean>
	<!-- The IoHandler implementation -->
	<bean id="minaHandler" class="com.missian.server.handler.MissianHandler">
		<constructor-arg>
			<bean class="com.missian.common.beanlocate.SpringLocator" />
		</constructor-arg>
	</bean>

	<!-- the IoFilters -->
	<bean id="executorFilter" class="org.apache.mina.filter.executor.ExecutorFilter" />
	<bean id="codecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter">
		<constructor-arg>
			<bean class="com.missian.server.codec.MissianCodecFactory" />
		</constructor-arg>
	</bean>
	<bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter">
		<property name="messageReceivedLogLevel" value="DEBUG"/>
		<property name="messageSentLogLevel" value="DEBUG"/>
		<property name="sessionCreatedLogLevel" value="DEBUG"/>
		<property name="sessionClosedLogLevel" value="DEBUG"/>
		<property name="sessionIdleLogLevel" value="DEBUG"/>
		<property name="sessionOpenedLogLevel" value="DEBUG"/>
	</bean>
	<!-- The non-SSL filter chain. -->
	<bean id="filterChainBuilder"
		class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
		<property name="filters">
			<map>
				<entry key="codecFilter" value-ref="codecFilter" />
				<entry key="executor" value-ref="executorFilter" />
				<entry key="loggingFilter" value-ref="loggingFilter" />
			</map>
		</property>
	</bean>
	<!-- The IoAcceptor which binds to port 1235 server side -->
	<bean id="minaAcceptor" class="org.apache.mina.transport.socket.nio.NioSocketAcceptor"
		init-method="bind" destroy-method="unbind">
		<property name="defaultLocalAddress" value=":1235" />
		<property name="handler" ref="minaHandler" />
		<property name="reuseAddress" value="true" />
		<property name="filterChainBuilder" ref="filterChainBuilder" />
	</bean>

	<!-- your business bean, missian client will call this bean by 'hello' -->
	<bean id="hello" class="com.missian.example.bean.HelloImpl"></bean>

</beans>
 

 

 

 

1
0
分享到:
评论
4 楼 gaojin 2014-04-01  
你的教程用到那些jar包?什么版本?
我表示新手有点摸不着头脑
3 楼 小树鹿鸣 2012-08-07  
这是什么问题 ?
2 楼 小树鹿鸣 2012-08-07  
引用
2012-08-07 15:51:54,453 DEBUG [org.apache.mina.core.filterchain.IoFilterEvent] - Firing a EXCEPTION_CAUGHT event for session 3
2012-08-07 15:51:54,453 WARN [org.apache.mina.filter.logging.LoggingFilter] - EXCEPTION :
org.apache.mina.filter.codec.ProtocolDecoderException: java.lang.OutOfMemoryError: Java heap space (Hexdump: 00 00 00 00 05 68 65 6C 6C 6F 00 00 00 1A 48 02 00 43 10 68 65 6C 6C 6F 5F 73 74 72 69 6E 67 5F 69 6E 74 92 02 68 79 AB)
at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:251)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:434)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.access$1200(DefaultIoFilterChain.java:46)
at org.apache.mina.core.filterchain.DefaultIoFilterChain$EntryImpl$1.messageReceived(DefaultIoFilterChain.java:796)
at org.apache.mina.core.filterchain.IoFilterAdapter.messageReceived(IoFilterAdapter.java:119)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.callNextMessageReceived(DefaultIoFilterChain.java:434)
at org.apache.mina.core.filterchain.DefaultIoFilterChain.fireMessageReceived(DefaultIoFilterChain.java:426)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.read(AbstractPollingIoProcessor.java:715)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:668)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:657)
at org.apache.mina.core.polling.AbstractPollingIoProcessor.access$600(AbstractPollingIoProcessor.java:68)
at org.apache.mina.core.polling.AbstractPollingIoProcessor$Processor.run(AbstractPollingIoProcessor.java:1141)
at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:64)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:39)
at java.nio.ByteBuffer.allocate(ByteBuffer.java:312)
at org.apache.mina.core.buffer.SimpleBufferAllocator.allocateNioBuffer(SimpleBufferAllocator.java:44)
at org.apache.mina.core.buffer.SimpleBufferAllocator.allocate(SimpleBufferAllocator.java:36)
at org.apache.mina.core.buffer.IoBuffer.allocate(IoBuffer.java:227)
at org.apache.mina.core.buffer.IoBuffer.allocate(IoBuffer.java:210)
at org.apache.mina.filter.codec.statemachine.FixedLengthDecodingState.decode(FixedLengthDecodingState.java:63)
at org.apache.mina.filter.codec.statemachine.DecodingStateMachine.decode(DecodingStateMachine.java:113)
at org.apache.mina.filter.codec.statemachine.DecodingStateProtocolDecoder.decode(DecodingStateProtocolDecoder.java:80)
1 楼 小树鹿鸣 2012-08-07  
引用
2012-08-07 15:51:54,296 DEBUG [com.missian.client.sync.SyncMissianProxy] - Missian[SyncMissianProxy [beanName=hello, port=8080, host=172.16.26.87]] calling hello_string_int

相关推荐

Global site tag (gtag.js) - Google Analytics