Mule 3.4.0中对Socket的应用
????? 使用mule模拟socket服务端处理socket客户端发送的消息的具体的实现:
mule-socket-config.xml
<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tcp="http://www.mulesoft.org/schema/mule/tcp" xmlns:file="http://www.mulesoft.org/schema/mule/file" xsi:schemaLocation=" http://www.mulesoft.org/schema/mule/tcp http://www.mulesoft.org/schema/mule/tcp/current/mule-tcp.xsd http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd"> <file:connector name="fileConnectorWithWorkDir" streaming="false" workDirectory="./logs/" pollingFrequency="500"/> <!-- 创建tcp对象 --> <tcp:connector name="TcpConnector" keepAlive="true" receiveBufferSize="2048" sendBufferSize="2048" receiveBacklog="500" serverSoTimeout="3000" keepSendSocketOpen="true" validateConnections="true"> <tcp:direct-protocol payloadOnly="true"/> </tcp:connector> <!-- 设置tcp的 host 和端口 --> <tcp:endpoint connector-ref="TcpConnector" name="Endpoint" host="127.0.0.1" port="5200" /> <!-- 模拟情况为: --> <model name="tcpModel"> <service name="tcpService"> <inbound> <tcp:inbound-endpoint name="inboundEndpoint" connector-ref="TcpConnector" host="127.0.0.1" port="5200" /> </inbound> <outbound> <pass-through-router> <file:outbound-endpoint path="./logs/" outputPattern="socket.txt"/> </pass-through-router> </outbound> </service> </model> </mule>
?
?
服务端:
import org.mule.api.MuleContext;import org.mule.api.MuleException;import org.mule.api.context.MuleContextFactory;import org.mule.config.spring.SpringXmlConfigurationBuilder;import org.mule.context.DefaultMuleContextFactory;/** * * 创建日期 2013-8-17<br> * @author $Author$<br> * @version $Revision$ $Date$ * @since 3.0.0 */public class MuleServerMain { public static void main(String[] args) { try { String configFile = "mule-socket-config.xml"; String[] configFileArr = new String[] {configFile }; MuleContextFactory muleContextFactory = new DefaultMuleContextFactory(); MuleContext context = muleContextFactory .createMuleContext(new SpringXmlConfigurationBuilder(configFileArr)); context.start(); } catch (MuleException t) { t.printStackTrace(); } }}
?
客户端代码:
import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.InetAddress;import java.net.Socket;/** * <p>模式<p> * * 创建日期 2013-8-19<br> * @author $Author$<br> * @version $Revision$ $Date$ * @since 3.0.0 */public class SocketClientMain { public static void main(String[] args) { try { Socket server = new Socket(InetAddress.getByName("127.0.0.1"), 5200); BufferedReader in = new BufferedReader(new InputStreamReader(server .getInputStream())); PrintWriter out = new PrintWriter(server.getOutputStream()); String str = "aaa"; for (int i = 0; i < 20; i++) { out.write(str+i+"\r\n"); } out.flush(); server.close(); } catch (Exception e) { e.printStackTrace(); } }}
?