首页 > 程序开发 > 综合编程 > 其他综合 >

SSM下整合netty使用队列进行文件传输

2017-04-20

SSM下整合netty使用队列进行文件传输:自己在闲暇无事的时候做的一些小demo,在这里分享出来给大家。

SSM下整合netty使用队列进行文件传输:自己在闲暇无事的时候做的一些小demo,在这里分享出来给大家。

@Service
@Scope("prototype")
public class PictureServiceImpl implements PictureService {

    @Value("${UPLOAD_PATH}")
    private String UPLOAD_PATH;

    @Value("${IMAGES_URL}")
    private String IMAGES_URL;

    @Value("${NETTY_URL}")
    private String NETTY_URL;

    @Value("${NETTY_PORT}")
    private Integer NETTY_PORT;

    @Autowired
    private FileThreadManager fileThreadManager;

    @Override
    public PictureResult uploadFile(MultipartFile file) {
        PictureResult pictureResult = new PictureResult();
        if (!file.isEmpty()) {
            try {
                String fileName = file.getOriginalFilename();
                String newname = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            fileThreadManager.start(NETTY_URL, NETTY_PORT);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                fileThreadManager.submit(file.getBytes(), newname, null);
                System.out.println("尝试获取数据");
                while (fileThreadManager.getFileInfoByName(newname) == null) {}
                FileInfo fileInfo = fileThreadManager.getFileInfoByName(newname);
                BeanUtils.copyProperties(fileInfo, pictureResult);
            } catch (Exception e) {
                e.printStackTrace();
                pictureResult.setError(1);
                pictureResult.setMessage("上传失败");
            }
        } else {
            pictureResult.setMessage("上传图片为空");
            pictureResult.setError(1);
        }
        return pictureResult;
    }

}

下面是主要的队列及netty的主要代码

@Component
@Scope("prototype")
public class FileThreadManager implements BeanFactoryAware{

    private BeanFactory beanFactory;

    private ExecutorService pool = Executors.newCachedThreadPool();

    private volatile ChannelFuture cf;

    private ConcurrentHashMap map=new ConcurrentHashMap<>();

    @Autowired
    private CustomChannelHandler customChannelHandler;

    public void submit(byte[] file,String fileNewName,String message){
        while(cf==null){}
        System.out.println("提交任务");
        FileThread fileThread = beanFactory.getBean(FileThread.class);
        fileThread.setFile(file);
        fileThread.setFileNewName(fileNewName);
        fileThread.setMessage(message);
        fileThread.setCf(cf);
        pool.execute(fileThread);
    }

    public void start(String nettyUrl,Integer nettyPort) throws InterruptedException{
        if(cf!=null)
            return ;
        EventLoopGroup workgroup = new NioEventLoopGroup();
        Bootstrap b = new Bootstrap();
        b.group(workgroup);
        b.channel(NioSocketChannel.class).handler(new ChannelInitializer() {

            @Override
            protected void initChannel(SocketChannel sc) throws Exception {
                // TODO Auto-generated method stub
                sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
                sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
                sc.pipeline().addLast(customChannelHandler);
                customChannelHandler.setMap(map);
            }
        });
        b.option(ChannelOption.SO_KEEPALIVE, true);
        ChannelFuture cf = b.connect(nettyUrl, nettyPort).sync();
        this.cf=cf;
        cf.channel().closeFuture().sync();
        workgroup.shutdownGracefully();
    }

    public FileInfo getFileInfoByName(String fileName) throws InterruptedException{
        Set> entrySet = map.entrySet();
        for (Entry entry : entrySet) {
            if(entry.getKey().trim().equals(fileName.trim())){
                return entry.getValue();
            }
        }
        return null;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        // TODO Auto-generated method stub
        this.beanFactory=beanFactory;
    }
}

最后说一下,FileThread,FileInfo和PictureResult等类都是自己自定义的类型,根据自己的需要从而定义。

相关文章
最新文章
热点推荐