<commit_before>package com.jeremie.spring.rpc.mina;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.apache.log4j.Logger;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.springframework.beans.factory.DisposableBean;

import java.net.InetSocketAddress;
import java.util.List;

/**
 * @author guanhong 15/10/25 下午4:08.
 */
public class MinaRPCBean implements DisposableBean {
    private static Logger logger = Logger.getLogger(MinaRPCBean.class);
    private String host = "127.0.0.1";
    private int serverPort = 8000;
    private IoSession session;
    private IoConnector connector;
    private boolean isConnect = false;

    public MinaRPCBean(EurekaClient discoveryClient){
        if (discoveryClient!=null){
            List<InstanceInfo> instances = discoveryClient.getInstancesByVipAddress("rpc-server",false);
            host = instances.get(0).getIPAddr();
        }
    }

    public IoSession getSession() {
        return session;
    }

    public boolean isConnect() {
        return isConnect;
    }

    public void init() {
        connector = new NioSocketConnector();
        connector.getFilterChain().addLast( "logger", new LoggingFilter(this.getClass()) );
        connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new ObjectSerializationCodecFactory()));
        connector.setHandler(new RPCClientHandler());
        ConnectFuture connectFuture = connector.connect(new InetSocketAddress(host,serverPort));
        //等待建立连接
        connectFuture.awaitUninterruptibly();
        session = connectFuture.getSession();
        isConnect = true;
    }

    @Override
    public void destroy() throws Exception {
        //关闭
        if(session!=null){
            if(session.isConnected()){
                session.getCloseFuture().awaitUninterruptibly();
            }
            connector.dispose(true);
        }
    }
}
<commit_msg>服务发现逻辑写到rpc初始化时运行<commit_after>package com.jeremie.spring.rpc.mina;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.apache.log4j.Logger;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.springframework.beans.factory.DisposableBean;

import java.net.InetSocketAddress;
import java.util.List;

/**
 * @author guanhong 15/10/25 下午4:08.
 */
public class MinaRPCBean implements DisposableBean {
    private static Logger logger = Logger.getLogger(MinaRPCBean.class);
    private String host = "127.0.0.1";
    private int serverPort = 8000;
    private IoSession session;
    private IoConnector connector;
    private boolean isConnect = false;
    private EurekaClient discoveryClient;

    public MinaRPCBean(EurekaClient discoveryClient){
        this.discoveryClient = discoveryClient;
    }

    public IoSession getSession() {
        return session;
    }

    public boolean isConnect() {
        return isConnect;
    }

    public void init() {
        if (discoveryClient!=null){
            List<InstanceInfo> instances = discoveryClient.getInstancesByVipAddress("rpc-server",false);
            host = instances.get(0).getIPAddr();
        }
        connector = new NioSocketConnector();
        connector.getFilterChain().addLast( "logger", new LoggingFilter(this.getClass()) );
        connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new ObjectSerializationCodecFactory()));
        connector.setHandler(new RPCClientHandler());
        ConnectFuture connectFuture = connector.connect(new InetSocketAddress(host,serverPort));
        //等待建立连接
        connectFuture.awaitUninterruptibly();
        session = connectFuture.getSession();
        isConnect = true;
    }

    @Override
    public void destroy() throws Exception {
        //关闭
        if(session!=null){
            if(session.isConnected()){
                session.getCloseFuture().awaitUninterruptibly();
            }
            connector.dispose(true);
        }
    }
}

{'fraction_non_alphanumeric': 0.10389055151774262, 'fraction_numerical': 0.00897819581017529, 'mean_word_length': 3.430871212121212, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6744706', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1330, 'n_words': 243}