spring mina整合(客戶端進行呼叫)
1.需要的包
mina-core-2.0.4.jar
mina-integration-beans-2.0.4.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
commons-logging-1.1.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
slf4j-log4j12-1.5.8.jar
slf4j-api-1.5.8.jar
log4j-1.2.14.jar
2.新建SpringMina web 專案,先看下我的目錄:
3.在web.xml中配置如下資訊:
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 上面配置的spring配置檔案的路徑,區分大小寫 -->
<param-value>
classpath:/config/spring/applicationContext-mina.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4.新建Source folder名為config,新建包名為config.spring,在此報下面建applicationContext-mina.xml檔案,內容如下(標頭檔案要加上):
<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>
<!-- 服務端處理 -->
<bean id="minaHandler" class="com.sk.mina.MinaProtocolHandler" />
<!-- the IoFilters -->
<bean id="executorFilter" class="org.apache.mina.filter.executor.ExecutorFilter" />
<bean id="mdcInjectionFilter" class="org.apache.mina.filter.logging.MdcInjectionFilter">
<constructor-arg value="remoteAddress" />
</bean>
<bean id="codecFilter" class="org.apache.mina.filter.codec.ProtocolCodecFilter">
<constructor-arg>
<bean class="org.apache.mina.filter.codec.textline.TextLineCodecFactory" />
</constructor-arg>
</bean>
<bean id="loggingFilter" class="org.apache.mina.filter.logging.LoggingFilter" />
<!-- The non-SSL filter chain. -->
<bean id="filterChainBuilder"
class="org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder">
<property name="filters">
<map>
<entry key="executor" value-ref="executorFilter" />
<entry key="mdcInjectionFilter" value-ref="mdcInjectionFilter" />
<entry key="codecFilter" value-ref="codecFilter" />
<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>
6.新建包名com.sk.mina,新建MinaProtocolHandler類(服務端應用程式),並且繼承IoHandlerAdapter類,內容如下:
private final Logger logger = LoggerFactory.getLogger(getClass());
public void messageReceived(IoSession session, Object message) {
String msg = (String) message;
System.out.println("Server Received: " + msg);
session.write("Server Send: " + msg);
//sessions.add(session);
}
@SuppressWarnings("deprecation")
public void sessionIdle(IoSession session, IdleStatus status) {
//disconnect an idle client
session.close();
}
@SuppressWarnings("deprecation")
public void exceptionCaught(IoSession session, Throwable cause) {
logger.warn("Unexpected exception.", cause);
session.close();
}
7.新建MinaClientHandler類(用來處理服務端發過來的資訊),並繼承IoHandlerAdapter類,內容如下:
private static final IoFilter LOGGING_FILTER = new LoggingFilter();
private static final IoFilter CODEC_FILTER = new ProtocolCodecFilter(
new TextLineCodecFactory());
@Override
public void sessionCreated(IoSession session) throws Exception {
session.getFilterChain().addLast("codec", CODEC_FILTER);
session.getFilterChain().addLast("logger", LOGGING_FILTER);
}
@Override
public void messageReceived(IoSession session, Object message)
throws Exception {
String msg = (String) message;
System.out.println("Client Received: " + msg);
}
8.新建MinaClientSupport類(負責傳送客戶端的請求到伺服器端),內容如下:
private IoHandler handler = null;
private IoSession session;
private String host;
private int port;
public boolean send(Object message) {
if (session != null && session.isConnected()) {
throw new IllegalStateException(
"Already connected. Disconnect first.");
}
SocketAddress address = new InetSocketAddress(host, port);
NioSocketConnector connector = new NioSocketConnector();
try {
connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());
connector.setHandler(handler);
ConnectFuture future1 = connector.connect(address);
future1.awaitUninterruptibly();
if (!future1.isConnected()) {
return false;
}
session = future1.getSession();
session.write(message);
return true;
} catch (Exception e) {
return false;
}
}
@SuppressWarnings("deprecation")
public void close() {
if (session != null) {
if (session.isConnected()) {
session.getCloseFuture().awaitUninterruptibly();
}
session.close();
}
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public void setHandler(IoHandler handler) {
this.handler = handler;
}
public static void main(String[] args) {
System.out.println("---------------------");
MinaClientSupport client = new MinaClientSupport();
MinaClientHandler handler = new MinaClientHandler();
client.setHandler(handler);
client.setHost("127.0.0.1");
client.setPort(1235);
String msg = "hello world!";
client.send(msg);
System.out.println("Client Send: " + msg);
client.close();
}
有什麼疑問歡迎進行討論,寫的不深入。
愛生活,愛分享,愛康寶!
相關文章
- spring系列—CAS客戶端與SpringSecurity整合Spring客戶端Gse
- 使用java呼叫fastDFS客戶端進行靜態資原始檔上傳JavaAST客戶端
- [Redis 客戶端整合] Java 中常用Redis客戶端比較Redis客戶端Java
- 使用Java客戶端對Redis進行操作Java客戶端Redis
- Spring Cloud實戰系列(二) - 客戶端呼叫Rest + RibbonSpringCloud客戶端REST
- webService 客戶端呼叫 axis2Web客戶端
- .net客戶端呼叫activeMQ程式碼客戶端MQ
- Spring Boot 整合 WebSocket 實現服務端推送訊息到客戶端Spring BootWeb服務端客戶端
- spring整合mina簡明教程Spring
- Spring Cloud實戰系列(三) - 宣告式客戶端呼叫FeignSpringCloud客戶端
- 微服務整合Spring Cloud Alibaba Seata (二) 客戶端連線微服務SpringCloud客戶端
- CouchBase C 客戶端介面呼叫例項客戶端
- 以 Laravel 的方式呼叫 ai 客戶端LaravelAI客戶端
- xfire 客戶端呼叫webservice的問題客戶端Web
- spring整合hessian進行遠端通訊Spring
- Spring Boot OAuth 2.0 客戶端Spring BootOAuth客戶端
- Spring Cloud構建客戶端SpringCloud客戶端
- 在網頁如何呼叫客戶端的可執行檔案網頁客戶端
- solaris11進行dns客戶端配置詳解DNS客戶端
- 使用 RAM 富客戶端進行資產工程設計客戶端
- .Net Remoting服務端與客戶端呼叫示例REM服務端客戶端
- 在指令碼中呼叫cassandra客戶端命令指令碼客戶端
- 環信3.0iOS客戶端的整合iOS客戶端
- 環信即時通訊——整合客戶端客戶端
- Redis:我是如何與客戶端進行通訊的Redis客戶端
- VNC客戶端是Windows,VNC客戶端是Windows如何進行操作VNC客戶端Windows
- 初識Spring Cloud Eureka(三)(Eureka客戶端之間 服務的相互呼叫)SpringCloud客戶端
- 環信3.0ios客戶端的整合(四)iOS客戶端
- Spring Cloud Config客戶端使用SpringCloud客戶端
- Spring Cloud Config—客戶端使用SpringCloud客戶端
- 在客戶端儲存對EJB的遠端呼叫是否可行?客戶端
- 如果呼叫遠端遠端url介面為https,且存在客戶端證書驗證,如何在客戶端處理HTTP客戶端
- iOS呼叫QQ客戶端發起臨時會話iOS客戶端會話
- Spring Cloud Ribbon 客戶端負載均衡SpringCloud客戶端負載
- 跟蹤客戶端執行的SQL客戶端SQL
- dubbo客戶端客戶端
- Pulsar客戶端客戶端
- mqtt 客戶端MQQT客戶端