|
| 1 | +package com.xwintop.xTransfer.receiver.service.impl; |
| 2 | + |
| 3 | +import com.xwintop.xTransfer.common.MsgLogger; |
| 4 | +import com.xwintop.xTransfer.common.model.LOGKEYS; |
| 5 | +import com.xwintop.xTransfer.common.model.LOGVALUES; |
| 6 | +import com.xwintop.xTransfer.common.model.Msg; |
| 7 | +import com.xwintop.xTransfer.messaging.*; |
| 8 | +import com.xwintop.xTransfer.receiver.bean.ReceiverConfig; |
| 9 | +import com.xwintop.xTransfer.receiver.bean.ReceiverConfigHdfs; |
| 10 | +import com.xwintop.xTransfer.receiver.service.Receiver; |
| 11 | +import com.xwintop.xTransfer.task.quartz.TaskQuartzJob; |
| 12 | +import lombok.extern.slf4j.Slf4j; |
| 13 | +import org.apache.commons.collections.MapUtils; |
| 14 | +import org.apache.commons.lang3.ArrayUtils; |
| 15 | +import org.apache.commons.lang3.StringUtils; |
| 16 | +import org.apache.hadoop.conf.Configuration; |
| 17 | +import org.apache.hadoop.fs.*; |
| 18 | +import org.springframework.context.annotation.Scope; |
| 19 | +import org.springframework.stereotype.Service; |
| 20 | + |
| 21 | +import java.io.ByteArrayOutputStream; |
| 22 | +import java.nio.file.NoSuchFileException; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Iterator; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +/** |
| 29 | + * @ClassName: ReceiverFsImpl |
| 30 | + * @Description: Fs接收器 |
| 31 | + * @author: xufeng |
| 32 | + * @date: 2018/6/13 16:11 |
| 33 | + */ |
| 34 | + |
| 35 | +@Service("receiverFs") |
| 36 | +@Scope("prototype") |
| 37 | +@Slf4j |
| 38 | +public class ReceiverHdfsImpl implements Receiver { |
| 39 | + private ReceiverConfigHdfs receiverConfigHdfs; |
| 40 | + private MessageHandler messageHandler; |
| 41 | + |
| 42 | + @Override |
| 43 | + public void receive(Map params) throws Exception { |
| 44 | + receiveInternalByFiles(receiverConfigHdfs.getPathIn(), params, 0); |
| 45 | + } |
| 46 | + |
| 47 | + private void receiveInternalByFiles(String pathIn, Map params, int receivedFileSum) throws Exception { |
| 48 | + pathIn = StringUtils.appendIfMissing(pathIn, "/", "/", "\\"); |
| 49 | + Configuration conf = new Configuration(); |
| 50 | + conf.set("fs.defaultFS", receiverConfigHdfs.getHdfsUrl()); |
| 51 | + FileSystem hdfs = FileSystem.get(conf); |
| 52 | + Path pathin = new Path(pathIn); |
| 53 | + if (!hdfs.exists(pathin) || !hdfs.isDirectory(pathin)) { |
| 54 | + log.error("path: " + pathIn + "not exist or not a dirctory!"); |
| 55 | + return; |
| 56 | + } |
| 57 | + RemoteIterator<FileStatus> pathIterator = hdfs.listStatusIterator(pathin); |
| 58 | + List<FileStatus> filePaths = new ArrayList<>(); |
| 59 | + try { |
| 60 | + while (pathIterator.hasNext()) { |
| 61 | + if (receiverConfigHdfs.getMax() >= 0 && receiverConfigHdfs.getMax() <= receivedFileSum) { |
| 62 | + break; |
| 63 | + } |
| 64 | + FileStatus curPath = pathIterator.next(); |
| 65 | + Path curFile = curPath.getPath(); |
| 66 | + if (curPath.isDirectory()) { |
| 67 | + String curFileName = curFile.getName(); |
| 68 | + if (StringUtils.isNotBlank(receiverConfigHdfs.getFileNameRegex())) { |
| 69 | + if (!curFileName.matches(receiverConfigHdfs.getFileNameRegex())) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + } |
| 73 | + if (receiverConfigHdfs.getMaxSize() != -1 && curPath.getLen() > receiverConfigHdfs.getMaxSize()) { |
| 74 | + log.warn(curFile.toString() + ": size is exceed the limit [" + receiverConfigHdfs.getMaxSize() + "]. fileSize:" + curPath.getLen()); |
| 75 | + continue; |
| 76 | + } |
| 77 | + if (receiverConfigHdfs.getDelayTime() > 0) { |
| 78 | + if (System.currentTimeMillis() - curPath.getModificationTime() < receiverConfigHdfs.getDelayTime()) { |
| 79 | + log.info("File is more new than currentTime-" + receiverConfigHdfs.getDelayTime()); |
| 80 | + continue; |
| 81 | + } |
| 82 | + } |
| 83 | + if (receiverConfigHdfs.getMinSize() != -1) { |
| 84 | + if (curPath.getLen() < receiverConfigHdfs.getMinSize()) { |
| 85 | + log.info("File's Size is too small then skip it,file name is:" + curFile.getName() + " fileSize:" + curPath.getLen()); |
| 86 | + continue; |
| 87 | + } |
| 88 | + } |
| 89 | + IMessage msg = new DefaultMessage(); |
| 90 | + String uuid = msg.getId(); |
| 91 | + log.info("begin receiving file:" + curFileName + "size=" + curPath.getLen() + ",id:" + uuid); |
| 92 | + if (!hdfs.exists(curFile)) { |
| 93 | + log.warn("curFile is not exist! file:" + curFile.toString()); |
| 94 | + continue; |
| 95 | + } |
| 96 | + try { |
| 97 | + try { |
| 98 | + FSDataInputStream is = hdfs.open(curFile); |
| 99 | + ByteArrayOutputStream fout = new ByteArrayOutputStream(); |
| 100 | + byte[] b = new byte[2048]; |
| 101 | + int len = 0; |
| 102 | + while ((len = is.read(b)) != -1) { |
| 103 | + fout.write(b, 0, len); |
| 104 | + } |
| 105 | + msg.setRawData(fout.toByteArray()); |
| 106 | + } catch (NoSuchFileException n) { |
| 107 | + log.warn("read optFile is not exist! file:" + curFile.toString()); |
| 108 | + continue; |
| 109 | + } |
| 110 | + msg.setFileName(curFileName); |
| 111 | + msg.setEncoding(receiverConfigHdfs.getEncoding()); |
| 112 | + msg.setProperty(LOGKEYS.CHANNEL_IN_TYPE, LOGVALUES.CHANNEL_TYPE_HDFS); |
| 113 | + msg.setProperty(LOGKEYS.CHANNEL_IN, pathIn); |
| 114 | + msg.setProperty(LOGKEYS.RECEIVER_TYPE, LOGVALUES.RCV_TYPE_FS); |
| 115 | + msg.setProperty(LOGKEYS.RECEIVER_ID, this.receiverConfigHdfs.getId()); |
| 116 | + if (MapUtils.isNotEmpty(receiverConfigHdfs.getArgs())) { |
| 117 | + msg.getProperties().putAll(receiverConfigHdfs.getArgs()); |
| 118 | + } |
| 119 | + IContext ctx = new DefaultContext(); |
| 120 | + ctx.setMessage(msg); |
| 121 | + |
| 122 | + Msg msgLogInfo = new Msg(LOGVALUES.EVENT_MSG_RECEIVED, uuid, null); |
| 123 | + msgLogInfo.put(LOGKEYS.CHANNEL_IN_TYPE, LOGVALUES.CHANNEL_TYPE_HDFS); |
| 124 | + msgLogInfo.put(LOGKEYS.CHANNEL_IN, pathIn); |
| 125 | + msgLogInfo.put(LOGKEYS.MSG_TAG, curFileName); |
| 126 | + msgLogInfo.put(LOGKEYS.MSG_LENGTH, ArrayUtils.getLength(msg.getMessage())); |
| 127 | + msgLogInfo.put(LOGKEYS.JOB_ID, params.get(TaskQuartzJob.JOBID)); |
| 128 | + msgLogInfo.put(LOGKEYS.JOB_SEQ, params.get(TaskQuartzJob.JOBSEQ)); |
| 129 | + msgLogInfo.put(LOGKEYS.RECEIVER_TYPE, LOGVALUES.RCV_TYPE_HDFS); |
| 130 | + msgLogInfo.put(LOGKEYS.RECEIVER_ID, this.receiverConfigHdfs.getId()); |
| 131 | + MsgLogger.info(msgLogInfo.toMap()); |
| 132 | + |
| 133 | + messageHandler.handle(ctx); |
| 134 | + } catch (Exception e) { |
| 135 | + log.error("ReceiverFs 读取文件失败:", e); |
| 136 | + } |
| 137 | + if (receiverConfigHdfs.isDelReceiveFile()) { |
| 138 | + hdfs.deleteOnExit(curFile); |
| 139 | + } |
| 140 | + log.info("received file:" + curFileName); |
| 141 | + receivedFileSum++; |
| 142 | + } else { |
| 143 | + if (receiverConfigHdfs.isIncludeSubdirectory() && curPath.isDirectory() && receivedFileSum < receiverConfigHdfs.getMax()) { |
| 144 | + filePaths.add(curPath); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } finally { |
| 149 | + if (hdfs != null) { |
| 150 | + hdfs.close(); |
| 151 | + } |
| 152 | + } |
| 153 | + //is directory then do |
| 154 | + if (receiverConfigHdfs.isIncludeSubdirectory() && receivedFileSum < receiverConfigHdfs.getMax()) { |
| 155 | + for (Iterator<FileStatus> it = filePaths.iterator(); it.hasNext(); ) { |
| 156 | + String curPathIn = it.next().getPath().toUri().getPath(); |
| 157 | + receiveInternalByFiles(curPathIn, params, receivedFileSum); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public void setReceiverConfig(ReceiverConfig receiverConfig) { |
| 164 | + this.receiverConfigHdfs = (ReceiverConfigHdfs) receiverConfig; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void setMessageHandler(MessageHandler messageHandler) { |
| 169 | + this.messageHandler = messageHandler; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void destroy() { |
| 174 | + |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments