Spring HTTP-Remoting Connector Builder
- This example is based on the subclassed objects example.
- The example is using a Spring-Boot server application.
- The Spring-Boot application is started in a Docker container.
Spring Server Configuration
- Docker Configuration.
- Create Docker Image.
- Java main server program contains the datasource configuration and starts Spring-Boot.
Docker Containerization Config
Docker Configuration : Dockerfile
FROM openjdk:8u212-jdk-alpine3.9
WORKDIR /app
COPY target/spring_http_remoting_server.war /app
EXPOSE 8080
CMD ["java", "-jar", "spring_http_remoting_server.war"]
Create Docker Image
The Docker Image is created during the Maven build process.XML-File : pom.xml (part)
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>docker-build</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>docker</executable>
<workingDirectory>${project.basedir}</workingDirectory>
<arguments>
<argument>build</argument>
<argument>-t</argument>
<argument>spring_http_remoting_server:${project.version}</argument>
<argument>.</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
...
Server Main Program
- Configure the Oracle datasource.
- Start the Spring-Boot server application.
Java Class : MainServer.java
package plsql_workspace_examples;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import oracle.ucp.jdbc.PoolDataSource;
import oracle.ucp.jdbc.PoolDataSourceFactory;
@Configuration
@EnableAutoConfiguration
@ComponentScan("impl")
public class MainServer {
@Bean
DataSource dataSource() throws SQLException
{
PoolDataSource poolDataSource = PoolDataSourceFactory.getPoolDataSource();
poolDataSource.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
poolDataSource.setURL(System.getenv("db.url"));
poolDataSource.setUser(System.getenv("db.username"));
poolDataSource.setPassword(System.getenv("db.password"));
poolDataSource.setInitialPoolSize(1);
poolDataSource.setMinPoolSize(2);
poolDataSource.setMaxPoolSize(10);
poolDataSource.setLoginTimeout(10);
poolDataSource.setInactiveConnectionTimeout(30);
poolDataSource.setTimeoutCheckInterval(15);
poolDataSource.setValidateConnectionOnBorrow(true);
poolDataSource.setConnectionWaitTimeout(60);
return poolDataSource;
}
public static void main(String[] args)
{
SpringApplication.run(MainServer.class, args);
}
}
Client Configuration
- Start the Docker Container including the Oracle datasource parameter.
- Java example calling the remote service.
Start Docker Image
- The Docker Image is started during the Maven client build process.
- The Oracle Database configuration parameter are set as environment variables.
XML-File : pom.xml (part)
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<!-- start docker -->
<execution>
<id>docker-start</id>
<phase>process-test-classes</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>docker</executable>
<workingDirectory>${project.basedir}</workingDirectory>
<arguments>
<argument>run</argument>
<argument>--name</argument>
<argument>spring_http_remoting_server</argument>
<argument>--rm</argument>
<argument>-p</argument>
<argument>12345:8080</argument>
<argument>-d</argument>
<argument>-e</argument>
<!-- configure the database url here -->
<argument>db.url=jdbc:oracle:thin:@192.168.0.109:1521/orcl</argument>
<argument>-e</argument>
<!-- configure the database username here -->
<argument>db.username=dbw_examples</argument>
<argument>-e</argument>
<!-- configure the database password here -->
<argument>db.password=dbw_examples</argument>
<argument>spring_http_remoting_server:${project.version}</argument>
</arguments>
</configuration>
</execution>
</plugin>
...
Calling the Remote Services
Java Class : SubclassedObjectsSpringApi.java
package plsql_workbench_examples.springapi;
import java.sql.Date;
import java.sql.Timestamp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
import service.SubclassedObjectsService;
import transferobject.BaseObject;
import transferobject.DerivedObject;
@Component
public class SubclassedObjectsSpringApi {
@Autowired
private SubclassedObjectsService subclassedObjectsService;
public static void main(String[] args)
{
System.setProperty("baseURL", "http://localhost:8080");
// Register Spring Beans, Spring Context and call demo method
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("factory")) {
ctx.register(SubclassedObjectsSpringApi.class);
SubclassedObjectsSpringApi demo = ctx.getBean(SubclassedObjectsSpringApi.class);
demo.runDemo();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void runDemo()
{
try {
int diff = 10;
// create a base object from the database function
BaseObject baseObject = subclassedObjectsService.getBaseObject();
System.out.format("java-inspect: %s [d:%s/ts:%s/instance:%s/version:%d.%d]%n",
baseObject.getClass(),
baseObject.d,
baseObject.ts,
baseObject.instance,
baseObject.dbVersion,
baseObject.dbRelease);
System.out.println("pl/sql inspect : " + subclassedObjectsService.inspectObject(baseObject));
// create an derived object from the database function
baseObject = subclassedObjectsService.getDerivedObject(diff);
DerivedObject derivedObject = (DerivedObject) baseObject;
System.out.format("java-inspect : %s [d:%s/ts:%s/instance:%s/version:%d.%d/newS:%s/newTs:%s/newD:%s/newN:%d]%n",
baseObject.getClass(),
baseObject.d,
baseObject.ts,
baseObject.instance,
baseObject.dbVersion,
baseObject.dbRelease,
derivedObject.newS,
derivedObject.newTs,
derivedObject.newD,
derivedObject.newN);
System.out.println("pl/sql inspect : " + subclassedObjectsService.inspectObject(baseObject));
// inspect of object instantiated in java
derivedObject = new DerivedObject();
derivedObject.d = new Date(System.currentTimeMillis());
derivedObject.instance = "any string";
derivedObject.ts = new Timestamp(System.currentTimeMillis());
derivedObject.dbVersion = 88;
derivedObject.dbRelease = 99;
derivedObject.newD = new Date(0);
derivedObject.newN = 1234567890;
derivedObject.newS = "another string";
derivedObject.newTs = new Timestamp((long) (Math.random() * 1000L * 3600L * 24L * 365L * 50L));
System.out.println("pl/sql inspect () : " + subclassedObjectsService.inspectObject(derivedObject));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Configuring and Running the Example
- The PL/SQL package has been installed in your Oracle database.
- Docker is installed and running.
- Configure the database connection parameter in the client/pom.xml.
- Just run mvn clean package from the Spring-Example parent folder.