no

Java Standalone Persistence using JPA

This application demonstrates the use of JPA in a standalone Java application. I. Getting Started This project is a standalone Java ap...


This application demonstrates the use of JPA in a standalone Java application.

I. Getting Started

This project is a standalone Java application that reads a server log file, parses and saves to a MySQL table access_log.

It also executes a named query that groups and filter the access log table via IP and threshold. It logs the grouped and filtered records in the console and another MySQL table named block_ip.

The project is created using maven and uses an entity manager to manage the entities. It compiles a project with all the dependencies and creates a jar named parser-jar-with-dependencies.jar in the target folder.

*Note that you must set your timezone to UTC.

II. Prerequisites

  • Java 8
  • MySQL or MariaDB
  • Maven
  • Installing
  • Clone the repository and let it download all the dependencies.

III. Running the tests

Before running the command the database must be reset first. Execute "sql/1 - schema.sql".

You must be inside the target folder before you execute the following commands.

java -jar parser-jar-with-dependencies.jar --startDate=2017-01-01.15:00:00 --duration=hourly --threshold=200 --accesslog=D:\Downloads\exercise\parser\src\main\resources\access.log
>The output will have 192.168.11.231 which has 200 or more requests between 2017-01-01.15:00:00 and 2017-01-01.15:59:59

java -jar parser-jar-with-dependencies.jar --startDate=2017-01-01.00:00:00 --duration=daily --threshold=500 --accesslog=D:\Downloads\exercise\\parser\src\main\resources\access.log
>The output will have 192.168.102.136 which has 500 or more requests between 2017-01-01.00:00:00 and 2017-01-01.23:59:59

The SQL used to create the schema and test the queries are stored in exercise/parser/sql folder.

Where
  • startDate - the lower data boundary when a line was created on the log file
  • duration - either hourly (1hour) or daily (24hours)
  • threshold - the minimum ip count given the date range
  • accesslog - path to log file

IV. Codes

AccessLog entity. This is where we will install the logs.
Notice that we have created a named query that we will use to filter a request given a date range and number of request for a given IP.

The PersistenceManager that we use to create a local entity manager factory resource with a given persistence.xml. And finally, here is the service class that:
  • Reads the log file
  • Saves the request log into a MySQL table
  • Filter the MySQL table given a date range and a minimum grouping count base on IP
  • Insert the IP that exceeds the request count on a given date range on another table

Related

coding 6933615913351442134

Post a Comment Default Comments

item