Skip to content

Commit 6104022

Browse files
author
Igor Polevoy
committed
references #215 Implement an asynch processing API based on HornetQ
1 parent 8086a07 commit 6104022

File tree

12 files changed

+605
-1
lines changed

12 files changed

+605
-1
lines changed

activeweb/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@
7070
<dependency>
7171
<groupId>commons-fileupload</groupId>
7272
<artifactId>commons-fileupload</artifactId>
73-
</dependency>
73+
</dependency>
7474
</dependencies>
7575
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.javalite.activeweb.async;
2+
3+
import java.net.InetAddress;
4+
import java.net.UnknownHostException;
5+
6+
/**
7+
* @author Igor Polevoy on 3/4/15.
8+
*/
9+
public class Host {
10+
11+
public static String get() {
12+
String res = "127.0.0.1";
13+
try {
14+
res = InetAddress.getLocalHost().getHostName();
15+
} catch (UnknownHostException ignored) {}
16+
return res;
17+
}
18+
}

javalite-hornet-nest/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Hornet Nest is an asynchronous processor for Java applications
2+
3+
This is a simple to use library for processing asynchronous jobs. It can be used in a web application to process
4+
jobs in a thread separate from the one that renders HMTL. It is also a genera purpose library that can be used in any
5+
non-web system.
6+
7+
Internal Implementation is based on [JBoss HornetQ](http://hornetq.jboss.org/).

javalite-hornet-nest/pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>javalite-hornet-nest</artifactId>
6+
<version>1.12-SNAPSHOT</version>
7+
<name>JavaLite - Hornet Nest</name>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>org.javalite</groupId>
12+
<artifactId>activeweb-root</artifactId>
13+
<version>1.12-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>org.slf4j</groupId>
24+
<artifactId>slf4j-simple</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.slf4j</groupId>
29+
<artifactId>slf4j-api</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.javalite</groupId>
34+
<artifactId>javalite-common</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.hornetq</groupId>
39+
<artifactId>hornetq-server</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.hornetq</groupId>
44+
<artifactId>hornetq-jms-client</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.hornetq</groupId>
48+
<artifactId>hornetq-jms-server</artifactId>
49+
</dependency>
50+
51+
</dependencies>
52+
53+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2009-2015 Igor Polevoy
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
18+
package org.javalite.hornet_nest;
19+
20+
/**
21+
* Implementations of this interface will have to know how to serialize themselves into
22+
* a string using a <code>toString()</code> method and how to de-serialize from string using <code>fromString(String)</code>
23+
* method.
24+
*
25+
* @author Igor Polevoy on 4/4/15.
26+
*/
27+
public interface Command {
28+
/**
29+
* Implementation will have to de-serialize from string.
30+
*
31+
* @param commandString String representation of a command. Can be XML, JSON or whatever you like,
32+
* so long as <code>toString()</code> produces a string and this method initializes a new
33+
* object with the same exact values.
34+
*/
35+
void fromString(String commandString);
36+
void execute();
37+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2009-2015 Igor Polevoy
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package org.javalite.hornet_nest;
18+
19+
import javax.jms.Message;
20+
import javax.jms.MessageListener;
21+
import javax.jms.TextMessage;
22+
23+
/**
24+
* @author Igor Polevoy on 4/5/15.
25+
*/
26+
public class CommandListener implements MessageListener{
27+
28+
@Override
29+
public void onMessage(Message message) {
30+
TextMessage tm = (TextMessage) message;
31+
try {
32+
String className = tm.getStringProperty("command_class");
33+
Command command = (Command) Class.forName(className).newInstance();
34+
command.fromString(tm.getText());
35+
onCommand(command);
36+
} catch (Exception e) {
37+
throw new HornetNestException("Failed to process command", e);
38+
}
39+
}
40+
41+
public <T extends Command> void onCommand(T command) {
42+
command.execute();
43+
}
44+
45+
}

0 commit comments

Comments
 (0)