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; } }
原文地址:https://www.jb51.cc/xml/297785.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。