加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

监听、接收mq消息、写入xml文件

发布时间:2020-12-16 06:38:45 所属栏目:百科 来源:网络整理
导读:package com.suning.search.data.receive.esb.listener;import java.io.BufferedWriter;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.text.SimpleDateFormat;import java.util.Date;i
package com.suning.search.data.receive.esb.listener;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.suning.search.common.util.SystemArgument;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.texen.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

public class MessageParse {
    
    public static  final Logger logger = LoggerFactory.getLogger(MessageParse.class);

    /**
     * 文件夹存放文件数目
     */
    private static  final int MAX_COUNT = 1000;
    
    private final static SimpleDateFormat TIME = new SimpleDateFormat("yyyyMMdd");
    
    /**
     * 当前目录名称
     */
    private static  String cuurDirName="";
    
    /**
     * 当前文件夹的文件数
     */
    private  int curDirNum = 0;
    
    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉 转换MQ的消息为字符串
     * 
     * @param message MQ消息
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public String getMessage(Message message) {
        String xml = "";
        try {
            if (message instanceof TextMessage) {
                TextMessage msg = (TextMessage) message;
                xml = msg.getText();
            } else if (message instanceof BytesMessage) {
                BytesMessage msg = (BytesMessage) message;
                byte[] buff = new byte[(int) msg.getBodyLength()];
                msg.readBytes(buff);
                xml = new String(buff);
            } else {
                logger.error("监听器接收的消息格式不对");
            }
        } catch (JMSException e) {
            logger.error("获取MQ消息出错",e);
        } catch (RuntimeException e) {
            logger.error("解析报文消息出错.",e);
        }
        return xml;
    }
    
    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉 向指定目录写文件
     *  单个线程
     * @param txt
     * @param path
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public void writeTxt(final String txt,String path) {
        FileWriter fw = null;
        BufferedWriter bf = null;
        PrintWriter out = null;
        File file  = null;
        String directoryName = getDirectoryName(path,MAX_COUNT);
        String name = path + directoryName + File.separator + SystemArgument.SDF_CHILD_PATH.format(new Date()) + ".xml";
        try {
            file = new File(name);
            if (!file.exists()) {
                file.createNewFile();
            }
            fw = new FileWriter(file);
            bf = new BufferedWriter(fw);
            out = new PrintWriter(bf);
            out.write(txt);
            out.flush();
            curDirNum=curDirNum+1;
        } catch (IOException e) {
            logger.error("写文件出现错误:" + txt,e);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (RuntimeException e) {
                    logger.error("写文件出现错误--》关闭PrintWriter出错:" + txt,e);
                }
            }
            if (bf != null) {
                try {
                    bf.close();
                } catch (IOException e) {
                    logger.error("写文件出现错误--》关闭BufferedWriter出错:" + txt,e);
                }
            }
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    logger.error("写文件出现错误--》关闭FileWriter出错:" + txt,e);
                }
            }
        }
    }
    
    /**
     * 
     * 功能描述: <br>
     * 选择文件夹
     * 
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public String getDirectoryName(String path,int max) {
        if (StringUtils.isEmpty(cuurDirName)) {
            cuurDirName = chooseDirectory(path,max);
            return cuurDirName;
        }
        if (curDirNum < max) {
            String time = TIME.format(new Date());
            String fileDate = "";
            if (StringUtils.isNotBlank(cuurDirName)) {
                fileDate = StringUtils.split(cuurDirName,"_")[0];
            }
            if (new File(path + cuurDirName).exists() && time.equals(fileDate)) {
                return cuurDirName;
            }
        }
        curDirNum = 0;
        String name = SystemArgument.SDF_CHILD_PATH.format(new Date());
        FileUtil.mkdir(path + File.separator + name);
        cuurDirName = name;
        return name;
    }
    
    /**
     * 
     * 功能描述: <br>
     * 选择文件夹
     * 
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public String chooseDirectory(String path,int max) {
        String name = "";
        File file = new File(path);
        if(!file.isDirectory()){
        	file.mkdir();
        }
        File[] list = file.listFiles();
        int size = list.length;
        for (int i = 0; i < size; i++) {
            if (list[i].isDirectory()) {
                String fileName = list[i].getName();
                if (StringUtils.isNotBlank(fileName)) {
                    String fileNamePre = StringUtils.split(fileName,"_")[0];
                    String date = TIME.format(new Date());
                    if (!fileNamePre.equals(date)) {
                        continue;
                    }
                }
                int n = getFileCount(path + File.separator + fileName);
                if (n < max) {
                    name = fileName;
                    break;
                }
            }
        }
        if (StringUtils.isBlank(name)) {
            name = SystemArgument.SDF_CHILD_PATH.format(new Date());
            FileUtil.mkdir(path + File.separator + name);
        }
        try {
            Thread.sleep(2); // 等待2毫秒. 避免文件名冲突
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return name;
    }
    
    /**
     * 取指定文件夹中文件数量
     * 
     * @return String
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public int getFileCount(String path) {
        int fileCount = 0;
        File d = new File(path);
        File[] list = d.listFiles();
        int size = list.length;
        for (int i = 0; i < size; i++) {
            if (list[i].isFile()) {
                fileCount++;
            }
        }
        return fileCount;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读