Business, Code, JavaEE, SOA, Technical, WebServices

Current Technology Research

Languages – used to solve problems that work with user interaction and machine development.

Kotlin for Java and Android.

Swift for IOS and Mac.
Cloud Technologies and Platforms – Infrastructure, functions and platforms that provide integrated solutions for systems and applications high availability and deployments.

Amazon Web Services (AWS).

Google Firebase

Understanding Domain Driven Design – is an approach to software development for complex needs by connecting the implementation to an evolving model.

http://en.wikipedia.org/wiki/Domain-driven_design

http://dddsample.sourceforge.net/

http://www.domainlanguage.com/


Reactive Programming
– is a programming paradigm oriented around data flows and the propagation of change.

http://en.wikipedia.org/wiki/Reactive_programming

http://news.dice.com/2014/01/13/how-is-reactive-different-from-procedural-programming/

https://www.youtube.com/watch?v=jIsxcXBWthI


Concurrency in Java
– is a form of computing in which several computations are executing during overlapping time periods –concurrently – instead of sequentially (one completing before the next starts). This is a property of a system – this may be an individual program, a computer, or a network – and there is a separate execution point or “thread of control” for each computation (“process”).

http://en.wikipedia.org/wiki/Concurrent_programming

http://en.wikipedia.org/wiki/Concurrency_in_java

http://tutorials.jenkov.com/java-concurrency/index.html

Hexagonal Architecture – Allow an application to equally be driven by users, programs, automated test or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases.

As events arrive from the outside world at a port, a technology-specific adapter converts it into a usable procedure call or message and passes it to the application. The application is blissfully ignorant of the nature of the input device. When the application has something to send out, it sends it out through a port to an adapter, which creates the appropriate signals needed by the receiving technology (human or automated). The application has a semantically sound interaction with the adapters on all sides of it, without actually knowing the nature of the things on the other side of the adapters.

http://alistair.cockburn.us/Hexagonal+architecture

Software Company

http://asp-software.org/www/misv_resources/marketing-articles/how-to-make-a-million-dollars-in-software/

 

Advertisements
Code, JavaEE, SOA, Technical, WebServices

Read Remote File Contents

@Test
public void formatOutput() {
    final String routerJS =
            "static/master/router.js";
    readRemoteFileContents(routerJS);
}

private void readRemoteFileContents(final String fileToRead) {
    HttpURLConnection connection = null;
    try {
        connection = (HttpURLConnection)githubURL(fileToRead).openConnection();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Scanner scanner = null;
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
        scanner = new Scanner(
                new StringReader(
                        reader.lines().collect(
                                Collectors.joining(System.getProperty("line.separator")))))
        .useDelimiter(System.getProperty("line.separator"));

        while(scanner.hasNext()) {
            System.out.printf("%s%n", scanner.next());
        }
    } catch (IOException e) {

    } finally {
        if (scanner != null) scanner.close();
        if (connection != null) connection.disconnect();
    }

}

private URL githubURL(final String fileToRead) throws MalformedURLException {
    final String baseGitHubFiles = "https://raw.githubusercontent.com/enterprisesaas/";
    final StringBuilder url = new StringBuilder(baseGitHubFiles).append(fileToRead);
    return new URL(url.toString());
}
Code, JavaEE, Technical

View Zip File Contents

@Test
public void viewCompressedStreamContents() {
    final String basePath = "/Server/path/";
    final String zip = basePath + "favicons.zip";

    try (InputStream zipStream = Files.newInputStream(Paths.get(zip))) {
        ZipInputStream buffered = new ZipInputStream(zipStream);
        ZipEntry entry;
        while ((entry = buffered.getNextEntry()) != null) {
            System.out.println(entry.toString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Technical

Concepts (Reactive Progamming, Domain Driven Design, Concurrency, Hexagonal)

1. Understanding Domain Driven Design – is an approach to software development for complex needs by connecting the implementation to an evolving model.

http://en.wikipedia.org/wiki/Domain-driven_design

http://dddsample.sourceforge.net/

http://www.domainlanguage.com/

2. Reactive Programming – is a programming paradigm oriented around data flows and the propagation of change.

http://en.wikipedia.org/wiki/Reactive_programming

http://news.dice.com/2014/01/13/how-is-reactive-different-from-procedural-programming/

https://www.youtube.com/watch?v=jIsxcXBWthI

https://speakerdeck.com/mgonto/reactive-angular

3. Concurrency in Java – is a form of computing in which several computations are executing during overlapping time periods –concurrently – instead of sequentially (one completing before the next starts). This is a property of a system – this may be an individual program, a computer, or a network – and there is a separate execution point or “thread of control” for each computation (“process”).

http://en.wikipedia.org/wiki/Concurrent_programming

http://en.wikipedia.org/wiki/Concurrency_in_java

http://tutorials.jenkov.com/java-concurrency/index.html

 

4. Hexagonal Architecture – Allow an application to equally be driven by users, programs, automated test or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases.

As events arrive from the outside world at a port, a technology-specific adapter converts it into a usable procedure call or message and passes it to the application. The application is blissfully ignorant of the nature of the input device. When the application has something to send out, it sends it out through a port to an adapter, which creates the appropriate signals needed by the receiving technology (human or automated). The application has a semantically sound interaction with the adapters on all sides of it, without actually knowing the nature of the things on the other side of the adapters.

http://alistair.cockburn.us/Hexagonal+architecture

5. Programming in the Large

https://speakerdeck.com/markhibberd/programming-in-the-large

Code, JavaEE, Technical

Embedded ActiveMQ

Send and receive a message using embedded active mq.

pom.xml


<?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>SelfContainedUnitTest</groupId>
<artifactId>SelfContainedUnitTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
<maven.compiler.plugin>2.3.2</maven.compiler.plugin>
<spring.version>4.0.1.RELEASE</spring.version>
<slf4j.version>1.6.1</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<activemq.version>5.8.0</activemq.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>

<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.12</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>${activemq.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgument>
-XDignore.symbol.file
</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>

</project>

ActiveMQTest.java


package embedded;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.jms.*;
import java.util.UUID;

/**
* Created by david.king
*/
public class ActiveMQTest {
private ActiveMQConnectionFactory activeMQConnectionFactory;
private Connection connection;
private Session session;
private MessageProducer messageProducer;
private MessageConsumer messageConsumer;
private Destination destination;
private String queueName = String.format("EmbeddedQueue%s", UUID.randomUUID().toString().substring(0,6));

@Test
public void spinUpBrokerAndSendAndReceiveMessage() throws Exception {
String message = "<message />";

// Send a message
send(message);

// Receive message
System.out.printf("Message Received: %s%n", receive());

}

private void send(String messageToSend) throws Exception {
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

TextMessage textMessage = session.createTextMessage(messageToSend);

producer.send(textMessage);

System.out.printf("JMS Message Sent: %s%n", textMessage);
}

private String receive() throws Exception {
MessageConsumer consumer = session.createConsumer(destination);

Message message = consumer.receive(3000);

TextMessage theTextMessage = (TextMessage) message;
String xml = theTextMessage.getText();
return xml;
}

@Before
public void setUp() throws Exception {
activeMQConnectionFactory =
new ActiveMQConnectionFactory(
"vm://localhost?broker.persistent=false");

connection = activeMQConnectionFactory.createConnection();

connection.start();

session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

destination = session.createQueue(queueName);
}

@After
public void tearDown() throws Exception {
session.close();
connection.close();
}
}

Code, JavaEE, SOA, Technical, WebServices

Hello World Restful Web Service Using Spring

Spring Framework provides all kinds of wonderful benefits to the modern developer. It has provided a way to build REST Web Services that is straightforward and productive. You only have to use what you need from Spring, its not an all or nothing framework.

In order to facilitate building our ‘Hello World’ service, we will first open IntelliJ IDEA 13 Community Edition and create a new project.

Create new project.

Select Maven as the project type and enter the project name as HelloWorldRestService as shown.

Enter project name.

Click the Next button and select the checkbox [] Create from archetype, then choose the maven-archetype-webapp, click next and finish. If asked, select to enable auto import for your maven settings.

Your project should look like the following.

Initial project view.

Replace your pom.xml file will the contents below.


<?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>HelloWorldRestService</groupId>
 <artifactId>HelloWorldRestService</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>war</packaging>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <java.version>1.7</java.version>
 <maven.compiler.plugin>2.3.2</maven.compiler.plugin>
 <spring.version>4.0.0.RELEASE</spring.version>
 </properties>

<dependencies>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.11</version>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>${spring.version}</version>
 </dependency>

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>${spring.version}</version>
 </dependency>

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-beans</artifactId>
 <version>${spring.version}</version>
 </dependency>

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-web</artifactId>
 <version>${spring.version}</version>
 </dependency>

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>${spring.version}</version>
 </dependency>

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-jms</artifactId>
 <version>${spring.version}</version>
 </dependency>

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-orm</artifactId>
 <version>${spring.version}</version>
 </dependency>

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-oxm</artifactId>
 <version>${spring.version}</version>
 </dependency>

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-test</artifactId>
 <version>${spring.version}</version>
 </dependency>

<dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.17</version>
 </dependency>

<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>1.7.2</version>
 <type>jar</type>
 </dependency>

<dependency>
 <groupId>com.google.code.gson</groupId>
 <artifactId>gson</artifactId>
 <version>2.2.2</version>
 </dependency>
 </dependencies>

<build>
 <plugins>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <version>${maven.compiler.plugin}</version>
 <configuration>
 <source>${java.version}</source>
 <target>${java.version}</target>
 <compilerArgument>
 -XDignore.symbol.file
 </compilerArgument>
 </configuration>
 </plugin>
 </plugins>
 </build>
</project>

Create Rest Controller

Create a new package named hello in /src/main/java. Create a new class named HelloRestController, then annotate the class with the @Controller annotation and enter the following code.

private final Logger logger = LoggerFactory.getLogger(HelloRestController.class);

@RequestMapping(value = "/helloworld", method = RequestMethod.GET)
 public @ResponseBody String displayTemplateJSON() throws Exception {
 logger.info("displayTemplateJSON() method invoked.");
 Map<String, Object> dataStructure = new HashMap<>();
 Map<String, Object> data = new HashMap<>();
 Gson gson = new Gson();

// build a simple data structure
 dataStructure.put("dataStructure", getStructure());
 logger.info("displayTemplateJSON(), building data structure.");

// convert Map to JSON
 String json = gson.toJson(dataStructure, Map.class);
 logger.info("displayTemplateJSON(), converting data structure to JSON.");

if (json != null) {
 logger.info("displayTemplateJSON(), returning data structure as JSON.");
 return json;
 }

return "\"{ \"No data foundl.\"}\"";
 }

 private Map<String, Object> getStructure() {
 Map<String, Object> data = new HashMap<>();
 data.put("Task", "Learn Spring Rest Web Services.");
 data.put("Company", "Example Inc.");

List<String> requestTypes = new ArrayList<>();
 requestTypes.add("RequestTypeOne");
 requestTypes.add("RequestTypeTwo");
 requestTypes.add("RequestTypeThree");
 requestTypes.add("RequestTypeFour");
 requestTypes.add("RequestTypeFive");
 requestTypes.add("RequestTypeSix");
 requestTypes.add("RequestTypeSeven");
 requestTypes.add("RequestTypeEight");
 data.put("RequestTypes", requestTypes);

logger.info("displayTemplateJSON(), data structure is: {}", data);
 return data;
 }

Configuration

You’re almost done. To ensure that Spring knows about your MVC Controller and outputs your logging, create a class named WebConfig, annotate it with @Configuration, @EnableWebMvc and @ComponentScan as shown.

intellij-webconfig

Next, create log4j.properties in /src/main/resources with the following content.

intellij-log4j

 

 

web.xml Configuration

A web.xml is required for this setup (note: there are ways to get rid of this as well, not covered in this article). Replace the contents of your web.xml with the following.


<?xml version="1.0" ?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 version="3.0">

<display-name>Web Application</display-name>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
 <param-name>contextClass</param-name>
 <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>hello.WebConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
 <servlet-name>myServlet</servlet-name>
 <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

Building and Deploying

Run maven clean and then install, deploy to your favorite Servlet container.

Invoke the rest service at: http://localhost:8080/HelloWorldRestService/helloworld

 

Enjoy.