Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.example.tcpserver;
- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.atomic.AtomicInteger;
- import org.apache.log4j.Logger;
- import org.springframework.integration.Message;
- import org.springframework.integration.channel.DirectChannel;
- import org.springframework.integration.support.MessageBuilder;
- /**
- * Reads Log file & sends each command by specific time.
- *
- */
- public class LogReader implements Runnable{
- /* Local logger. */
- private static final Logger LOG = Logger.getLogger(LogReader.class);
- private final SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss.S");
- private long timeToWait = 500 ;
- private String command;
- private String path2File;
- DirectChannel channel;
- @SuppressWarnings("rawtypes")
- private final Message origMsg;
- private AtomicInteger counter = new AtomicInteger();
- public LogReader(DirectChannel channel, @SuppressWarnings("rawtypes") Message origMsg) {
- this.channel = channel;
- this.origMsg = origMsg;
- }
- /**
- * Reads & sends commands to the client.
- */
- private void read(){
- FileInputStream fstream = null;
- String line;
- try {
- /* Creates an input stream to be read. */
- fstream = new FileInputStream(getPath2File());
- /* Wraps an input stream in order to be able reading of a whole line */
- DataInputStream in = new DataInputStream(fstream);
- @SuppressWarnings("resource")
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- while ((line = br.readLine()) != null) {
- command = line;
- sendAndLog(timeToWait);
- }
- } catch (Exception e) {
- LOG.error("Error: " + e.getMessage());
- } finally {
- LOG.info("Transmission finished.");
- /* Closed the stream */
- if(fstream != null) {
- try {
- fstream.close();
- } catch (IOException e) {
- LOG.error(e);
- }
- }
- }
- }
- /**
- *
- * @param value
- */
- private void sendAndLog(long value){
- sleep(value);
- LOG.info("t:" + command + " at " + dateFormat.format(Calendar.getInstance().getTime()) + "\n");
- channel.send(MessageBuilder.withPayload(command.getBytes()).copyHeaders(origMsg.getHeaders()).build());
- }
- /**
- * Waits for awhile as provided parameter.
- *
- * @param timeInMillisec to be waited.
- */
- private static void sleep(long timeInMillisec){
- try {
- TimeUnit.MILLISECONDS.sleep(timeInMillisec);
- } catch (InterruptedException e) {
- LOG.error(e);
- }
- }
- //=======================================
- // Getters/Setters
- //=======================================
- /**
- * Gets a {@link java.text.SimpleDateFormat}.
- *
- * @return a date formatter.
- */
- public SimpleDateFormat getDateFormat() {
- return dateFormat;
- }
- /**
- * Gets a command.
- *
- * @return a command.
- */
- public String getCommand() {
- return command;
- }
- /**
- * Sets a command.
- *
- * @param command to be set.
- */
- public void setCommand(String command) {
- this.command = command;
- }
- public String getPath2File() {
- return path2File;
- }
- public void setPath2File(String path2File) {
- this.path2File = path2File;
- }
- public void run() {
- LOG.info("\r***************************************\rGoing to read file ...["
- + counter.incrementAndGet()
- + "]\r***************************************\n");
- read();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement