I have a project that works perfectly if I deploy it outside of a JAR. However, it is unable to get sessions when its deployed as a JAR. Here are the artifacts:
Main.java:
public class Main {
public static void main(String[] args) throws Exception{
JndiContext registry=new JndiContext();
CamelContext context=new DefaultCamelContext(registry);
context.addRoutes(new RouteBuilder() {
#Override
public void configure() {
try{
from("jetty:http://0.0.0.0:7700?matchOnUriPrefix=true&sessionSupport=true").bean(HtmlProcessor.class);
}
catch(Exception e){
e.printStackTrace();
}
}
});
context.start();
Thread.sleep(1000000);
context.stop();
}
}
HtmlProcessor.java:
public class HtmlProcessor {
public String login(Exchange exchange){
HttpSession session=exchange.getIn(HttpServletRequest.class).getSession();
Integer count=(Integer)session.getAttribute("count");
if (count==null) count=0;
count++;
session.setAttribute("count", count);
StringBuilder sb=new StringBuilder();
sb.append("<html><body><form>");
sb.append(count);
sb.append("<BR><input type=text name=username></form></body></html>");
return sb.toString();
}
}
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>user1</groupId>
<artifactId>jettysession</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<camel.version>2.10.3</camel.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jetty</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>assemble</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifestEntries>
<Bundle-ClassPath>.</Bundle-ClassPath>
<Main-Class>user1.Main</Main-Class>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Here is the meaningful part of the exception:
java.lang.NullPointerException
at user1.HtmlProcessor.login(HtmlProcessor.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:341)
...
There is a file in META-INF/services/org/apache/camel/ named TypeConverter, which you need to merge with data from all the JARs. When you do this uber JAR I am pretty sure the TypeConverter file is possible just the last file, and therefore you lose data.
See this FAQ at Camel: http://camel.apache.org/how-do-i-use-a-big-uber-jar.html
Related
I am trying to use my Raspberry Pi 4's GPIOs to turn on various thing in my smart home.
I am getting 2.6V on the GPIO Input, thus resulting in a HIGH signal.
I have confirmed this with a basic python script, but since I am better in Java and I want to use some APIs, I wanted to use Java and thus Pi4J.
Whatever I am doing however, the Inputs are never changing their state. Or rather, the Listener for the input is never triggered. But the console does print statements generally, the console works.
Here my code:
public class App {
private final static int inputOne = 27; // PIN 13, address 27
private final static Context pi4j = com.pi4j.Pi4J.newAutoContext();
public static void main(String[] args) throws InterruptedException, IOException {
final Console console = new Console();
console.promptForExit();
// I/O Config Build
var isInput = DigitalInput.newConfigBuilder(pi4j).id("0").name("inputOne").address(inputOne)
.pull(PullResistance.PULL_DOWN).debounce(3000L).provider("pigpio-digital-input").build();
// I/O Build
var inputOne = pi4j.din().create(isInput);
inputOne.addListener(e -> {
if (e.state() == DigitalState.HIGH) {
console.print("input high");
} else {
}
});
console.waitForExit();
pi4j.shutdown();
}
}
I have also tried using the Board address of the pin instead of the board layout, did result in nothing as well.
My pom.xml, I am using Pi4J v2:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Pi_SmartHomeLogic</groupId>
<artifactId>Pi_SmartHomeLogic</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>app.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!--<repository> -->
<!-- <id>caarmen-repo</id>-->
<!--<url>https://dl.bintray.com/caarmen/maven/</url> -->
<!--</repository> -->
<dependencies>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.2</version>
<type>maven-plugin</type>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0</version>
<type>maven-plugin</type>
</dependency>
<dependency>
<groupId>ca.rmen</groupId>
<artifactId>lib-sunrise-sunset</artifactId>
<version>1.1.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.github.zeroone3010</groupId>
<artifactId>yetanotherhueapi</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-gpio-extension</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-raspberrypi</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-pigpio</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>
</project>
Since Pi4J does no longer use wiringPi, it uses the BCM address instead, so I dont get why it doesnt work?
I had the same problem. For me it helped to change the line:
var inputOne = pi4j.din().create(isInput);
to (like in the example):
DigitalInputProvider provider = pi4j.provider("pigpio-digital-input");
var inputOne = provider.create(isInput);
and to run the java program as user root (see The big sudo challenge: how can we fix the need to run PiGpio provider applications as sudo?). Then it will recognize the changed state of the input pin.
When running the program as user pi I get the error:
com.pi4j.library.pigpio.PiGpioException: PIGPIO ERROR: PI_INIT_FAILED; pigpio initialisation failed
at com.pi4j.library.pigpio.impl.PiGpioBase.validateResult(PiGpioBase.java:265) ~[pi4j-library-pigpio-2.2.1.jar:?]
at com.pi4j.library.pigpio.impl.PiGpioBase.validateResult(PiGpioBase.java:251) ~[pi4j-library-pigpio-2.2.1.jar:?]
at com.pi4j.library.pigpio.impl.PiGpioNativeImpl.gpioInitialise(PiGpioNativeImpl.java:95) ~[pi4j-library-pigpio-2.2.1.jar:?]
at com.pi4j.library.pigpio.PiGpio.initialize(PiGpio.java:159) ~[pi4j-library-pigpio-2.2.1.jar:?]
at com.pi4j.plugin.pigpio.provider.gpio.digital.PiGpioDigitalInputProviderImpl.create(PiGpioDigitalInputProviderImpl.java:60) ~[pi4j-plugin-pigpio-2.2.1.jar:?]
at com.pi4j.plugin.pigpio.provider.gpio.digital.PiGpioDigitalInputProviderImpl.create(PiGpioDigitalInputProviderImpl.java:41) ~[pi4j-plugin-pigpio-2.2.1.jar:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
at com.pi4j.provider.impl.ProviderProxyHandler.invoke(ProviderProxyHandler.java:100) ~[pi4j-core-2.2.1.jar:?]
... 9 more
Just to be sure, can you extend your listener a bit like this
inputOne.addListener(e -> {
console.print(e.state());
if (e.state() == DigitalState.HIGH) {
console.print("input high");
}
});
This example describes all the steps for such an example application: https://pi4j.com/getting-started/minimal-example-application/
I am using the blade library in a java 18 project. See code below.
public class Application {
public static void main(String[] args) {
String path = switch (args[0]) {
case "test" -> "/test";
default -> "/default";
};
Blade.create()
.listen(8081)
.get(path, new MyHandler())
.start();
}
private static class MyHandler implements RouteHandler {
#Override
public void handle(RouteContext context) {
context.text("hello");
}
}
}
This code is throwing an exception:
java.lang.IllegalArgumentException: Unsupported class file major version 62
at org.objectweb.asm.ClassReader.(ClassReader.java:176)
at org.objectweb.asm.ClassReader.(ClassReader.java:158)
at org.objectweb.asm.ClassReader.(ClassReader.java:146)
at org.objectweb.asm.ClassReader.(ClassReader.java:284)
at com.hellokaton.blade.asm.ASMUtils.findMethodParmeterNames(ASMUtils.java:30)
at com.hellokaton.blade.mvc.handler.RouteActionArguments.getRouteActionParameters(RouteActionArguments.java:39)
at com.hellokaton.blade.mvc.RouteContext.injectParameters(RouteContext.java:551)
at com.hellokaton.blade.server.RouteMethodHandler.handle(RouteMethodHandler.java:75)
at com.hellokaton.blade.server.HttpServerHandler.executeLogic(HttpServerHandler.java:149)
at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:646)
at java.base/java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:482)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:469)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at java.base/java.lang.Thread.run(Thread.java:833)
This library is written in java 8. I thought that using such switch pattern with enums is incompatible with java 8. However, if I replace one line like in the example below, then it will be executed successfully. Switch-enum pattern hasn't been removed.
// Blade.get() signature:
// public Blade get(String path, RouteHandler handler);
Blade.create()
.listen(8081)
.get(path, new MyHandler()) // <- old (exception)
.start();
Blade.create()
.listen(8081)
.get(path, context -> new MyHandler().handle(context)) // <- new (why works?)
.start();
Using lambda expression instead of object solved the problem. Why didn't the new switch-enum pattern break the code, and what is the reason for the exception then?
Interface RouteHandler looks like this:
#FunctionalInterface
public interface RouteHandler {
void handle(RouteContext var1);
}
My pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github</groupId>
<artifactId>test</artifactId>
<version>1.0-snapshot</version>
<packaging>var</packaging>
<name>test Maven Webapp</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>18</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>com.hellokaton</groupId>
<artifactId>blade-core</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>test</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
I want to use Tika as a dependency in a Maven project, to extract metadatas from files. It's working fine when I run the class with mvn exec:java, but not with java -cp, so I suspect it is a dependency problem...
I included all the dependencies in the jar with the maven shade plugin, and at build they are included.
The pom.xml:
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.company.myapp</groupId>
<artifactId>metadata-extractor</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Metadata Extractor</name>
<url>http://maven.apache.org</url>
<properties>
<tika.version>1.19</tika.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Tika -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>${tika.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Main class:
public class App
{
public static void main( String[] args )
{
// Get path
Path path = Paths.get("/path/to/image.jpg");
// Use Tika
TikaConfig tikaConfig = TikaConfig.getDefaultConfig();
Metadata metadata = new Metadata();
AutoDetectParser parser = new AutoDetectParser(tikaConfig);
ContentHandler handler = new BodyContentHandler(-1);
try {
TikaInputStream stream = TikaInputStream.get(path, metadata);
parser.parse(stream, handler, metadata, new ParseContext());
} catch (IOException | SAXException | TikaException e) {
System.out.println("error: " + e.toString());
return;
}
// Prints the metadata and content...
System.out.println("Parsed Metadata: ");
System.out.println(metadata);
System.out.println("Parsed Text: ");
System.out.println(handler.toString());
}
}
Result, with mvn exec:java (working as expected):
Parsed Metadata:
... X-Parsed-By=org.apache.tika.parser.DefaultParser X-Parsed-By=org.apache.tika.parser.jpeg.JpegParser ... other metadatas ...
Parsed Text:
But, with:
mvn clean package
java -cp target/metadata-extractor-1.0-SNAPSHOT.jar org.company.myapp.App
I got:
Parsed Metadata:
X-Parsed-By=org.apache.tika.parser.EmptyParser resourceName=image.jpg Content-Length=1557172 Content-Type=image/jpeg
Parsed Text:
What am I doing wrong? How do I have to build the project for it to correctly autodetect the parser?
Thanks.
There is no parser in your classpath so EmptyParser is chosen. I think the problem is in shade plugin. Remove this line:
<minimizeJar>true</minimizeJar>
And add these dependencies with proper version:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>jbig2-imageio</artifactId>
</dependency>
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-core</artifactId>
</dependency>
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-jpeg2000</artifactId>
</dependency>
I have a project folder but it is not a java project. It is a maven project. I have written a junit test and it runs perfectly when running in the eclipse IDE but when I run the maven command mvn install, it seems to skip my junit tests. I have included the test file in src/test/java/ (the name of my test is AppTest.java) and the main .java file (with the main method) is in src/main/java/. I have noticed that the project I am currently working on is a maven project and not a maven java project. I have included a screen of my current folder structure:
folder structure
Maven test output <- should not build as I have a deliberate test that should fail
This is the POM. I have deleted/commented out some sensitive parts so the pom file may be syntactically wrong but the main plugins I use are there; tap4j, junit and surefire.
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>integration-api-parent</artifactId>
<groupId>uk.gov.dwp</groupId>
<version>1.0.2</version>
</parent>
<artifactId>aa</artifactId>
<version>1.0.6</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>aa</finalName>
<plugins>
<!-- plugin>
<groupId>com.github.fracpete</groupId>
<artifactId>latex-maven-plugin</artifactId>
<configuration>
<forceBuild>true</forceBuild>
</configuration>
</plugin>
<plugin>
<groupId>com.github.fracpete</groupId>
<artifactId>latex-maven-plugin</artifactId>
<configuration>
<forceBuild>true</forceBuild>
</configuration>
</plugin-->
<plugin>
<!-- Plug-in utilised for the execution of the JMeter Integration Tests -->
<!-- These tests are executed against the nominated integration server where as -->
<!-- instance of AA exists -->
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>1.9.0</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
<configuration>
<ignoreResultErrors>false</ignoreResultErrors>
<suppressJMeterOutput>false</suppressJMeterOutput>
<overrideRootLogLevel>INFO</overrideRootLogLevel>
</configuration>
</plugin>
<plugin>
<!-- Step to copy the latest plug-ins that form this build to the integration server -->
<!-- This is done using the SCP command via the ANT plug-in thus allowing it to execute on all platforms -->
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<dependency>
<groupId>ant</groupId>
<artifactId>ant-jsch</artifactId>
<version>1.6.5</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.42</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.tap4j/tap4j -->
<dependency>
<groupId>org.tap4j</groupId>
<artifactId>tap4j</artifactId>
<version>4.2.1</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
<!-- plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/cassandra-assembly.xml</descriptor>
<descriptor>src/main/assembly/devenv-assembly.xml</descriptor>
<descriptor>src/main/assembly/main-assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
<!-- plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>templating-maven-plugin</artifactId>
<configuration>
<skipPoms>false</skipPoms>
<sourceDirectory>${project.basedir}/src/main/latex-templates</sourceDirectory>
<outputDirectory>${project.build.directory}/latex</outputDirectory>
</configuration>
</plugin-->
</plugins>
</build>
</project>
AppTest:
package AccessGateway;
import static org.junit.Assert.*;
import java.io.File;
import org.junit.Test;
import org.tap4j.consumer.TapConsumer;
import org.tap4j.consumer.TapConsumerFactory;
import org.tap4j.model.TestSet;
public class AppTest {
Practise prac;
final String DIRECTORY = "C:\\Users\\Hello\\Desktop\\";
#Test
public void testHeaderProcessor() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"TAPHeaderProcessor.txt")));
}
#Test
public void testHeaderPortForward() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"TAPHeaderPortForward.txt")));
}
#Test
public void catunittest() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"catunittest.txt")));
}
#Test
public void catunitcrowstest() {
prac = new Practise();
assertFalse(prac.runTest(new File(DIRECTORY+"catcrowd.txt")));
}
#Test
public void testCrowd() {
prac = new Practise();
assertFalse(
prac.runTest(new File(DIRECTORY+"TAPCrowd.txt")));
}
#Test
public void testADFS() {
prac = new Practise();
assertFalse(
prac.runTest(new File(DIRECTORY+"TAPADFSformat.txt")));
}
}
The problem is the packaging of your project which is pom
You can't execute Surefire on this kind of project.
Try adding surefire plugin. When i have tests in my app i always include it (works for junit as well as testng). Based on your logs i can see that you dont have it declared.
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
OS: Linux
Vert.x Version: 3.3.3
Machines: Two different with
different IP within same subnet
I want to enable metrics monitoring for an simple ping-pong application, where I can watch how many packets were sent between the two nodes.
The measured metrics should be pushed over the eventbus and consumed by a website, providing a dashboard. Copied from the vertx examples on Github
Starting apps with following commands
Sender:
vertx run de.avm.boundary.Sender -cluster -cp target/vertx-ping-pong-3.3.3-fat.jar -Dvertx.metrics.options.enabled=true
Receiver
vertx run de.avm.boundary.Receiver -cluster -cp target/vertx-ping-pong-3.3.3-fat.jar
doesn't provide any metrics.
Source Code
Sender.java
package de.avm.boundary;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.dropwizard.MetricsService;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.handler.sockjs.BridgeOptions;
import io.vertx.ext.web.handler.sockjs.PermittedOptions;
import io.vertx.ext.web.handler.sockjs.SockJSHandler;
public class Sender extends AbstractVerticle {
Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());
#Override
public void start() {
Vertx.currentContext().config();
System.out.println("START SENDER VERTICLE DEPLOYMENT ID: " + deploymentID());
BridgeOptions bridgeOptions = new BridgeOptions().
addOutboundPermitted(
new PermittedOptions().
setAddress("metrics-sender")
).addOutboundPermitted(new PermittedOptions().
setAddressRegex("metrics-receiver")
);
Router router = Router.router(vertx);
router.route("/eventbus/*").handler(SockJSHandler.create(vertx).bridge(bridgeOptions));
router.route().handler(StaticHandler.create());
HttpServer httpServer = vertx.createHttpServer();
httpServer.requestHandler(router::accept).listen(8080);
//why is the service object null ??
MetricsService service = MetricsService.create(vertx.eventBus());
System.out.println("Metrics-Service: " + service);
vertx.setPeriodic(100, new Handler<Long>() {
#Override
public void handle(Long timerID) {
vertx.eventBus().publish("ping-address", "more news! at: " + System.currentTimeMillis());
}
});
vertx.setPeriodic(1000, new Handler<Long>() {
#Override
public void handle(Long timerID) {
JsonObject metrics = service.getMetricsSnapshot(vertx);
vertx.eventBus().publish("metrics-sender", metrics);
System.out.println("Metrics: " + metrics);
}
});
}
}
Receiver.java
package de.avm.boundary;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.dropwizard.MetricsService;
public class Receiver extends AbstractVerticle {
MetricsService service = MetricsService.create(vertx);
#Override
public void start() {
System.out.println("START RECEIVER VERTICLE DEPLOYMENT ID: " + deploymentID());
vertx.eventBus().consumer("ping-address", new Handler<Message<String>>() {
#Override
public void handle(Message<String> message) {
// Reply to it
System.out.println("Received message: " + message.body());
message.reply("pong!");
}
}).completionHandler(new Handler<AsyncResult<Void>>() {
#Override
public void handle(AsyncResult<Void> event) {
if (event.succeeded()) {
System.out.println("Eventbus registration complete!");
}
}
});
// Send a metrics events every second
vertx.setPeriodic(1000, t -> {
JsonObject metrics = service.getMetricsSnapshot(vertx.eventBus());
vertx.eventBus().publish("metrics-receiver", metrics);
});
}
}
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.avm</groupId>
<artifactId>vertx-ping-pong</artifactId>
<version>3.3.3</version>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-dropwizard-metrics</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-hazelcast</artifactId>
<version>${project.version}</version>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-lang-js</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>io.vertx.core.Launcher</Main-Class>
<Main-Verticle>de.avm.boundary.Sender</Main-Verticle>
</manifestEntries>
</transformer>
</transformers>
<artifactSet/>
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
How can I enable Metrics?
Solved the problem by changing starting appliaction, especially the Sender an Receiver Verticles, in another war.
The best solution for me was starting the application with
Sender:
java -jar target/vertx-ping-pong-3.3.3-Sender-fat.jar -cluster -Dvertx.metrics.options.enabled=true
Receiver
java -jar target/vertx-ping-pong-3.3.3-Receiver-fat.jar -cluster -Dvertx.metrics.options.enabled=true
The pitfall is, that you must tell maven, especially the shade plugin, which them main class is. I have achieved this by defining a placeholder within the properties part in the pom.xml and passing the name of the main Vericle during maven build execution.
mvn package -DmainClass=Sender
This also results to place a fat-jar withing the target folder where the name of the passed parameter is included in the file name.
Here the modified pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.avm</groupId>
<artifactId>vertx-ping-pong</artifactId>
<version>3.3.3</version>
<properties>
<runWithClass>${mainClass}</runWithClass>
</properties>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-dropwizard-metrics</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-hazelcast</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-lang-js</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>io.vertx.core.Launcher</Main-Class>
<Main-Verticle>de.avm.boundary.${runWithClass}</Main-Verticle>
</manifestEntries>
</transformer>
</transformers>
<artifactSet/>
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-${runWithClass}-fat.jar</outputFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>