Skip to content

Commit b3232be

Browse files
author
Robin Duda
committed
Improve package structure + logging & added commandline import option.
1 parent 6ef4463 commit b3232be

14 files changed

+163
-83
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ElasticSearch and Kibana (version 5.0.0) should not require any additional confi
1010
## Running
1111
Running the application,
1212
```
13-
java -jar excelastic-1.0.1.jar run Launcher
13+
java -jar excelastic-1.0.2.jar
1414
```
1515

1616
If any connection errors occur check that the ElasticSearch listen port matches with the elastic_port in the configuration file. Make sure that ElasticSearch is running by directing your browser at [localhost:9200](http://localhost:9200/_count). When the server is started browse to [localhost:8080](http://localhost:8080/) for the web interface.

pom.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>com.codingchili</groupId>
1212
<artifactId>excelastic</artifactId>
13-
<version>1.0.1</version>
13+
<version>1.0.2</version>
1414
<build>
1515
<plugins>
1616
<plugin>
@@ -37,7 +37,7 @@
3737
<transformers>
3838
<transformer
3939
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
40-
<mainClass>io.vertx.core.Launcher</mainClass>
40+
<mainClass>com.codingchili.Launcher</mainClass>
4141
</transformer>
4242
</transformers>
4343
</configuration>
@@ -50,32 +50,32 @@
5050
<dependency>
5151
<groupId>junit</groupId>
5252
<artifactId>junit</artifactId>
53-
<version>RELEASE</version>
53+
<version>4.12</version>
5454
<scope>test</scope>
5555
</dependency>
5656

5757
<dependency>
5858
<groupId>io.vertx</groupId>
5959
<artifactId>vertx-core</artifactId>
60-
<version>3.3.3</version>
60+
<version>3.4.1</version>
6161
</dependency>
6262

6363
<dependency>
6464
<groupId>io.vertx</groupId>
6565
<artifactId>vertx-unit</artifactId>
66-
<version>3.3.3</version>
66+
<version>3.4.1</version>
6767
</dependency>
6868

6969
<dependency>
7070
<groupId>io.vertx</groupId>
7171
<artifactId>vertx-web</artifactId>
72-
<version>3.3.3</version>
72+
<version>3.4.1</version>
7373
</dependency>
7474

7575
<dependency>
7676
<groupId>io.vertx</groupId>
7777
<artifactId>vertx-web-templ-jade</artifactId>
78-
<version>3.3.3</version>
78+
<version>3.4.1</version>
7979
</dependency>
8080

8181
<dependency>

src/main/java/Launcher.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/main/java/Controller/Website.java renamed to src/main/java/com/codingchili/Controller/Website.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package Controller;
1+
package com.codingchili.Controller;
22

3-
import Model.*;
3+
import com.codingchili.Model.*;
44
import io.vertx.core.*;
55
import io.vertx.core.buffer.Buffer;
66
import io.vertx.core.json.JsonObject;
@@ -12,17 +12,19 @@
1212
import java.io.PrintWriter;
1313
import java.io.StringWriter;
1414
import java.util.Iterator;
15+
import java.util.logging.Logger;
1516

1617
/**
1718
* @author Robin Duda
19+
*
20+
* Manages the web interface and handles file uploads.
1821
*/
1922
public class Website extends AbstractVerticle {
23+
private Logger logger = Logger.getLogger(getClass().getName());
2024
private static final String DONE = "/done";
2125
private static final String ERROR = "/error";
2226
private static final String MESSAGE = "message";
2327
private static final String OFFSET = "offset";
24-
private static final String INDEX = "index";
25-
private static final String ITEMS = "items";
2628
private static final String FILE = "file";
2729
private static final String NO_FILE_WAS_UPLOADED = "No file was uploaded.";
2830
private Vertx vertx;
@@ -43,8 +45,14 @@ public void start(Future<Void> start) {
4345
router.route("/static/*").handler(StaticHandler.create());
4446
router.route("/*").handler(TemplateHandler.create(engine));
4547

46-
vertx.createHttpServer().requestHandler(router::accept).listen(Configuration.WEB_PORT);
47-
start.complete();
48+
vertx.createHttpServer().requestHandler(router::accept).listen(Configuration.WEB_PORT, done -> {
49+
if (done.succeeded()) {
50+
logger.info("Started website on port " + Configuration.WEB_PORT);
51+
start.complete();
52+
} else {
53+
start.fail(done.cause());
54+
}
55+
});
4856
}
4957

5058
private void setRouterAPI(Router router) {
@@ -80,20 +88,14 @@ private String traceToText(Throwable throwable) {
8088
private void parse(Buffer buffer, MultiMap params) throws ParserException {
8189
try {
8290
int columnRow = Integer.parseInt(params.get(OFFSET));
83-
String index = params.get(INDEX);
8491
FileParser parser = new FileParser(buffer.getBytes(), columnRow);
85-
86-
JsonObject result = new JsonObject()
87-
.put(ITEMS, parser.toJsonArray())
88-
.put(INDEX, index.toLowerCase());
89-
90-
sendOutput(result);
92+
sendOutput(parser.toImportableObject());
9193
} catch (NumberFormatException e) {
9294
throw new ParserException(e);
9395
}
9496
}
9597

9698
private void sendOutput(JsonObject data) {
97-
vertx.eventBus().send(Configuration.BUS_TRANSACTIONS, data);
99+
vertx.eventBus().send(Configuration.INDEXING_ELASTICSEARCH, data);
98100
}
99101
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.codingchili;
2+
3+
import java.util.Optional;
4+
import java.util.logging.Level;
5+
import java.util.logging.Logger;
6+
7+
import com.codingchili.Controller.Website;
8+
import com.codingchili.Model.Configuration;
9+
import com.codingchili.Model.ElasticWriter;
10+
import com.codingchili.Model.FileParser;
11+
import com.codingchili.Model.ParserException;
12+
13+
import io.vertx.core.*;
14+
15+
/**
16+
* @author Robin Duda
17+
*
18+
* Launcher class to bootstrap the application.
19+
*/
20+
public class Launcher {
21+
private static final Logger logger = Logger.getLogger(Launcher.class.getName());
22+
private static final String COMMAND_IMPORT = "import";
23+
private Vertx vertx = Vertx.vertx();
24+
25+
public static void main(String[] args) {
26+
new Launcher(args);
27+
}
28+
29+
public Launcher(String[] args) {
30+
logger.info("Starting application..");
31+
Future<Void> future = Future.future();
32+
33+
future.setHandler(done -> {
34+
if (done.succeeded()) {
35+
logger.info("Successfully started application");
36+
getImportFileName(args).ifPresent(this::importFile);
37+
} else {
38+
logger.log(Level.SEVERE, "Failed to start application", done.cause());
39+
}
40+
});
41+
42+
start(future);
43+
}
44+
45+
private void importFile(String fileName) {
46+
logger.info(String.format("Loading file %s from filesystem..", fileName));
47+
vertx.fileSystem().readFile(fileName, file -> {
48+
if (file.succeeded()) {
49+
logger.info("Parsing XLSX file to JSON..");
50+
try {
51+
FileParser parser = new FileParser(file.result().getBytes(), 1);
52+
logger.info("File parsed, starting import to elasticsearch..");
53+
vertx.eventBus().send(Configuration.INDEXING_ELASTICSEARCH, parser.toImportableObject());
54+
} catch (ParserException e) {
55+
logger.log(Level.SEVERE, String.format("Failed to import file %s", fileName));
56+
}
57+
} else {
58+
logger.log(Level.SEVERE, String.format("Failed to load file %s", fileName), file.cause());
59+
}
60+
});
61+
}
62+
63+
private static Optional<String> getImportFileName(String[] args) {
64+
for (int i = 0; i < args.length; i++) {
65+
switch (args[i]) {
66+
case COMMAND_IMPORT:
67+
return Optional.of(args[++i]);
68+
}
69+
}
70+
return Optional.empty();
71+
}
72+
73+
public void start(Future<Void> start) {
74+
Future<String> writer = Future.future();
75+
Future<String> website = Future.future();
76+
vertx.deployVerticle(new ElasticWriter(), writer.completer());
77+
vertx.deployVerticle(new Website(), website.completer());
78+
79+
CompositeFuture.all(writer, website).setHandler(done -> {
80+
if (done.succeeded()) {
81+
start.complete();
82+
} else {
83+
start.fail(done.cause());
84+
}
85+
});
86+
}
87+
}

src/main/java/Model/Configuration.java renamed to src/main/java/com/codingchili/Model/Configuration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package Model;
1+
package com.codingchili.Model;
22

33
import io.vertx.core.json.JsonObject;
44

@@ -8,12 +8,14 @@
88

99
/**
1010
* @author Robin Duda
11+
*
12+
* Handles application configuration.
1113
*/
1214
public class Configuration {
1315
public static final int ELASTIC_PORT = configuration().getInteger("elastic_port");
1416
public static final String ELASTIC_HOST = configuration().getString("elastic_host");
1517
public static final int WEB_PORT = configuration().getInteger("web_port");
16-
public static final String BUS_TRANSACTIONS = "bus.transactions";
18+
public static final String INDEXING_ELASTICSEARCH = "bus.transactions";
1719
private static JsonObject configuration;
1820

1921
private static JsonObject configuration() {

src/main/java/Model/ElasticWriter.java renamed to src/main/java/com/codingchili/Model/ElasticWriter.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package Model;
1+
package com.codingchili.Model;
2+
3+
import java.util.logging.Logger;
24

35
import io.vertx.core.AbstractVerticle;
46
import io.vertx.core.Context;
@@ -9,8 +11,14 @@
911

1012
/**
1113
* @author Robin Duda
14+
*
15+
* Writes json data to elasticsearch for indexing.
1216
*/
1317
public class ElasticWriter extends AbstractVerticle {
18+
private static final String INDEX = "index";
19+
private static final String BULK = "/_bulk";
20+
private static final String ITEMS = "items";
21+
private Logger logger = Logger.getLogger(getClass().getName());
1422
private Vertx vertx;
1523

1624
@Override
@@ -20,21 +28,27 @@ public void init(Vertx vertx, Context context) {
2028

2129
@Override
2230
public void start(Future<Void> start) {
23-
processTransactions();
24-
31+
startSubmitListener();
32+
logger.info("Started elastic writer");
2533
start.complete();
2634
}
2735

28-
private void processTransactions() {
29-
vertx.eventBus().consumer(Configuration.BUS_TRANSACTIONS, handler -> {
36+
private void startSubmitListener() {
37+
vertx.eventBus().consumer(Configuration.INDEXING_ELASTICSEARCH, handler -> {
38+
logger.info("Received file object, starting import..");
3039
JsonObject data = (JsonObject) handler.body();
31-
String index = data.getString("index");
40+
String index = data.getString(INDEX);
3241

3342
vertx.createHttpClient().post(
34-
Configuration.ELASTIC_PORT, Configuration.ELASTIC_HOST, index + "/_bulk")
35-
.handler(response -> response.bodyHandler(body -> {
36-
}))
37-
.end(bulkQuery(data.getJsonArray("items"), index));
43+
Configuration.ELASTIC_PORT, Configuration.ELASTIC_HOST, index + BULK)
44+
.handler(response -> {
45+
logger.info(String.format("Submitted with result [%d] %s",
46+
response.statusCode(),
47+
response.statusMessage()));
48+
49+
response.bodyHandler(body -> {});
50+
})
51+
.end(bulkQuery(data.getJsonArray(ITEMS), index));
3852
});
3953
}
4054

src/main/java/Model/FileParser.java renamed to src/main/java/com/codingchili/Model/FileParser.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package Model;
1+
package com.codingchili.Model;
22

33
import io.vertx.core.json.JsonArray;
44
import io.vertx.core.json.JsonObject;
@@ -13,11 +13,16 @@
1313

1414
/**
1515
* @author Robin Duda
16+
*
17+
* Parses xlsx files into json objects.
1618
*/
1719
public class FileParser {
20+
public static final String INDEX = "index";
21+
public static final String ITEMS = "items";
1822
private JsonArray list = new JsonArray();
1923
private int columns;
2024
private int items;
25+
private int offset;
2126

2227
/**
2328
* Parses the contents of an XLSX into JSON.
@@ -31,6 +36,7 @@ public FileParser(byte[] bytes, int offset) throws ParserException {
3136
XSSFWorkbook workbook = new XSSFWorkbook(new ByteArrayInputStream(bytes));
3237
XSSFSheet sheet = workbook.getSheetAt(0);
3338

39+
this.offset = offset;
3440
this.columns = getColumnCount(sheet.getRow(offset));
3541
this.items = getItemCount(sheet, offset);
3642

@@ -40,8 +46,8 @@ public FileParser(byte[] bytes, int offset) throws ParserException {
4046
}
4147
}
4248

43-
public JsonArray toJsonArray() {
44-
return list;
49+
public JsonObject toImportableObject() {
50+
return new JsonObject().put(ITEMS, list).put(INDEX, offset);
4551
}
4652

4753
private void readRows(XSSFSheet sheet, int columnRow) {

src/main/java/Model/ParserException.java renamed to src/main/java/com/codingchili/Model/ParserException.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
package Model;
1+
package com.codingchili.Model;
22

33
/**
44
* @author Robin Duda
5+
*
6+
* Parser exception to be thrown when an error during xlsx parsing occurs.
57
*/
68
public class ParserException extends Throwable {
79

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Manifest-Version: 1.0
2-
Main-Class: io.vertx.core.Launcher
2+
Main-Class: com.codingchili.Launcher
33

0 commit comments

Comments
 (0)