Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sample for vertx-mysql-postgresql-client #333

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions jasync-examples/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
= Vert.x Simplest vertx-mysql-postgresql-client project

This project shows a very simple hello world Vert.x project using vertx-mysql-postgresql-client, which has a simple HTTP server which
returns the result of 'SELECT 0' on mysql db.

Under the hood vertx-mysql-postgresql-client uses jasync-sql as the database driver.

In this example Vert.x is used embedded. I.e. we use the Vert.x APIs directly in our own classes rather than deploying
the code in verticles.

You can run or debug the example in your IDE by just right clicking the main class and run as.. or debug as...

The pom.xml uses the Maven shade plugin to assemble the application and all it's dependencies into a single "fat" jar.

To run with maven

mvn compile exec:java

To build a "fat jar"

mvn package

To run the fat jar:

java -jar target/jasync-examples-3.6.3-fat.jar

(You can take that jar and run it anywhere there is a Java 8+ JDK. It contains all the dependencies it needs so you
don't need to install Vert.x on the target machine).

You can also run the fat jar with maven:

mvn package exec:exec@run-app

Now point your browser at http://localhost:8080
131 changes: 131 additions & 0 deletions jasync-examples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?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>io.vertx</groupId>
<artifactId>jasync-examples</artifactId>
<version>3.6.3</version>

<properties>
<!-- the main class -->
<exec.mainClass>io.vertx.example.HelloWorldEmbedded</exec.mainClass>
</properties>

<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-mysql-postgresql-client-jasync</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>

</dependencies>

<build>

<pluginManagement>
<plugins>
<!-- We specify the Maven compiler plugin as we need to set it to Java 1.8 -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>

<!--
You only need the part below if you want to build your application into a fat executable jar.
This is a jar that contains all the dependencies required to run it, so you can just run it with
java -jar
-->
<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>${exec.mainClass}</Main-Class>
</manifestEntries>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource>
</transformer>
</transformers>
<artifactSet>
</artifactSet>
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar
</outputFile>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<executions>
<execution>
<!-- run the application using the fat jar -->
<id>run-app</id>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>target/${project.artifactId}-${project.version}-fat.jar</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>staging</id>
<repositories>
<repository>
<id>staging</id>
<url>https://oss.sonatype.org/content/repositories/iovertx-3814/</url>
</repository>
</repositories>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.vertx.example;

import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.asyncsql.MySQLClient;
import io.vertx.ext.sql.SQLClient;
import io.vertx.ext.sql.SQLConnection;


/**
* @author oshai
*/
public class HelloWorldEmbedded {

public static void main(String[] args) {
JsonObject mySQLClientConfig = new JsonObject()
.put("username", "mysql_async")
.put("password", "root")
.put("database", "mysql_async_tests")
.put("port", 33018)
.put("host", "localhost");
SQLClient mySQLClient = MySQLClient.createShared(Vertx.vertx(), mySQLClientConfig);

// Create an HTTP server which simply returns "Hello World!" to each request.
Vertx.vertx().createHttpServer()
.requestHandler(req -> {
mySQLClient.getConnection(res -> {
if (res.succeeded()) {

SQLConnection connection = res.result();

// Got a connection
connection.query("SELECT 0", result ->
req.response().end("Got response " + result.result().getNumRows()));
} else {
// Failed to get connection - deal with it
req.response().end("Failed!");
}
});

})
.listen(8080);
}

}