Editor’s note: We found an interesting series of articlesLearn 30 New Technologies in 30 Days, is being translated, an update a day, year-end gift package. Here’s day 28.


Today on Learn 30 New Technologies in 30 Days, I decided to introduce OpenShift’s Eclipse integration. The application will run on OpenShift, and we will use OpenShift’s Eclipse plug-in to develop and deploy an application.

Prepare for

  1. Basic Java knowledge.

  2. Install the latest version of the JDK on your system. You can install both OpenJDK 7 and Oracle JDK 7. OpenShift supports OpenJDK6 and 7.

  3. Download the latest Eclipse package from the official website. At the time of this writing, the latest Eclipse package is called Kepler.

Installing Eclipse is easy, just unzip it.

tar -xzvf eclipse-jee-kepler-R-*.tar.gz

1. Install the OpenShfit Eclipse plugin

Open Eclipse and enter your project workspace. Open the Help > Eclipse Marketplace and you will see the following screen.

Search for “JBoss Tools” in the search box. The first result is “JBoss Tools(Kepler).”

Clicking Install will bring up a list of selected plug-ins to install. Since the purpose of this article is to demonstrate, we’ll just select JBoss OpenShift Tools. Select and click “OK”.

Now we need to accept the license agreement. Select Accept and click Finish.

Eclipse warns that the plug-in is unsigned. Click “OK” and Eclipse will ask for a reboot, click “Yes” and reboot.

2. Create an OpenShift application

Once the plug-in is installed, creating an OpenShift application is easy.

In Eclipse, click File > New > Other > OpenShift Application and click “Next” on the page shown below:

You need to fill out your OpenShift account information. If you haven’t signed up yet, you can click “Sign Up Now” and a wizard screen will guide you to sign up.

Next, create an OpenShift domain name. Each account has one and only one unique domain name. The domain name forms part of the URL that OpenShift assigns to the application. For example, if your application is the name of “sharemylocation”, “onopenshiftcloud” is your domain name, then the application of the URL is http://sharemylocation-onopenshiftcloud.rhcloud.com.

Then we need to upload the SSH key to openshift. SSH key for Git operations and access the app Gear.

We can upload an existing one or create a new one. Click the New button to create a New one.

We need to provide the key name and the file name of the private key and public key.

Then the app creation wizard appears, and we need to fill in some of the details of the app. Note the selection of MongoDB.

Then we need to configure the ShareMyLocation application and server. Just choose the default.

You then ask for some information about your Git repository. Just leave the default options.

Finally, click the “Finish” button. This will create an application container (GEAR) for us, automatically configuring SELinux and cgroup. OpenShift will also create a private Git repository and clone it locally. DNS is then configured. OpenShift will also install MongoDB.

Finally, the project is imported into the Eclipse workspace as a Maven project.

After the import, you are asked if you want to push the uncommitted code to OpenShift.

You might be thinking, you’re not changing the app, so why are you asking whether to push it? The reason is that when the project is imported into Eclipse, JBoss creates a.jsdtscope file under the.settings folder. Since this file is not ignored, the OpenShift Eclipse plug-in asks if it is deployed. You can set this file to be ignored by visiting Window > Show View > Other > Git > Git Staging.

We can view our online application in OpenShift’s Explore View. Window > Show View > Other > JBoss Tools > OpenShift Explorer

Right-click the application and click Web Browser to open the template application in the default browser.

Understand the development workflow

  1. Write functional code to commit to Git through Git Staging view. Git Staging views provide a visual interface that makes it easy to see and compare the changes we make.

  2. Under Git Staging View, we have two options. Either we just do a local commit, or we push it to the remote at the same time. After pushing to remote oh, OpenShift will build our application after death.

4. The first change

To better understand the above workflow, let’s make a small change — change index.html:

<h1>
    OpenShift MongoDB Backbone.js Tutorial
</h1>

Visit Git Staging View to see the changes:

Drag Changes to Pretensed Changes to compose a commit message.

Click the Commit button to Commit the changes. As mentioned above, do not use Commit and Push to trigger application deployment without showing the build log. Build logs are useful, especially when builds fail.

Access Server View:

Right-click the Application Server and click Publish:

A confirmation box will appear, and click Yes.

A Console screen appears showing the progress of the build:

To see the application Server for JBoss EAP, just right-click SharemyLocation under Server View and select Tail Files.

All log files, including database log files, are defaulted to TAIL. We will configure only the tail server.log file.

This opens a new Console screen that displays only JBoss EAP Server.log

Finally, we can view the changes In the Browser by right-clicking the SharemyLocation server and then clicking Show In > Web Browser.

We can see the changes in the default browser.

5. Hot deployment

By default, once you publish your application or do a Git push, OpenShift will stop services like MongoDB and JBoss EAP Server, do a Maven build, and then restart the services and start deployment. This is time consuming and not conducive to rapid development. We can speed things up by enabling hot deployment. Hot deployment simply replaces files without stopping the service.

Right-click on the project to access the OpenShift > Consee Markers to activate hot deployment.

Then you need to do some configuration. We see that Java 7 is enabled by default. If it is disabled, the application will use Java 6.

We enable Hot Deploy. This creates a hot_deploy empty file under the.openshift/markers folder.

Next, when we visit the Git Staging view, we can see that the hot_deploy file is in Unstaged Changes.

Drag it to Passage Changes, write the Commit message, and click the Commit button to Commit.

Go to the Server view to publish the changes. The build log shows that hot deployment is enabled.

Not stopping cartridge jbosseap because hot deploy is enabled Not stopping cartridge mongodb because hot deploy is enabled Building jbosseap cartridge Scaling down Maven heap settings due to presence of hot_deploy marker Found pom.xml... Attempting to Build with 'MVN-E Clean Package -Popenshift -DskipTests' Attempting to Build with' MVN-E Clean Package -Popenshift -DskipTests' Apache Maven 3.0.3 (R1075437; 2011-06-20 13:22:37-0400) Maven Home: /etc/Alternatives/Maven-3.0 Java Version: 1.7.0_45, vendor: Oracle Corporation Java Home: /usr/lib/ JVM/java-1.7.0-OpenJDK-1.7.0.45 / JRE Default locale: en_US, platform encoding: Ansi_x3.4-1968 OS name: "Linux ", version: "2.6.32-358.23.2.el6.x86_64", arch: "i386", family: "unix" [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building sharemylocation 1.0 [INFO] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- (INFO) Using encoding to utf-8 copy filtered resources. [INFO] skip non existing resourceDirectory /var/lib/openshift/526f931ce0b8cd94f300003f/app-root/runtime/repo/src/test/resources [INFO] [INFO] --- Maven-compiler-plugin :2.3.2:testCompile (default-testCompile) @SharemyLocation -- [INFO] Packaging webapp [INFO] Assembling webapp [sharemylocation] in [/var/lib/openshift/526f931ce0b8cd94f300003f/app-root/runtime/repo/target/sharemylocation] [INFO] Processing war project  [INFO] Building war: /var/lib/openshift/526f931ce0b8cd94f300003f/app-root/runtime/repo/deployments/ROOT.war [INFO] WEB-INF/web.xml already added, skipping [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS Starting application sharemylocation Not starting cartridge mongodb because hot deploy is enabled Deploying jbosseap cartridge Not starting cartridge jbosseap because hot deploy is enabled

Similarly, the Server log will show that it simply replaced the WAR file. This is a second deployment.

2013/10/30 03:31:57, 174 INFO/org. Jboss. As the server deployment] (MSC service thread 1-2) JBAS015877: Stopped deployment ROOT.war (runtime-name: ROOT. War) in 260 ms 2013/10/30 03:31:57, 200 INFO [org. Jboss. As the server deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "ROOT.war" (runtime-name: "Root.war ") 2013/10/30 03:31:57,316 INFO [org.jboss.web] (ServerService Thread Pool -- 67) JBAS018210: Register Web Context: 2013/10/30 03:31:57,630 INFO [org.jboss.as. Server] (DeploymentScanner-Threads-2) JBAS018565: Replaced deployment "ROOT.war" with deployment "ROOT.war"

Great! You get feedback very quickly after you change the code. It saves a lot of time and improves our production efficiency.

Upgrade to Java 7

While OpenShift uses Java 7 to build projects, Maven projects still use Java 6. Modify the following properties in the pom.xml to upgrade to Java 7.

Piler < maven.com. Source > 1.7 < / maven.com piler. Source > < maven.com piler. Target > 1.7 < / maven.com piler. Target >

After making the changes, right-click on the project and Maven > Update.

7. Delete template files

Java EE versions 6 and above, web.xml is optional. We can configure most components using annotations. We will remove the web.xml, health.jsp, snop.jsp, and Image folders. The Git staging view will show the changes and we can commit them.

8. Update the Maven WAR plugin

If you release it now, the build will fail. Because the Maven WAR plug-in defaults to web.xml. The latest version of the Maven WAR plug-in adds a configuration property. The failonMissingWebXML attribute tells the WAR plug-in that the build should not fail just because web.xml does not exist.

Modified pom. XML:

< plugin > < artifactId > maven - war - the plugin < / artifactId > < version > 2.4 < / version > < configuration > <failOnMissingWebXml>false</failOnMissingWebXml> <outputDirectory>deployments</outputDirectory> <warName>ROOT</warName> </configuration> </plugin>

Use the Git Staging view to commit changes. And then publish.

9. Write PingResource

We’ll write a simple RESTful service that will respond to {‘ping’: ‘pong’} when/API /v1/ping receives a request to make sure everything works properly.

We will use JAX-RS to write RESTful services. JAX-RS defines a set of Annotation-driven APIs that can be used to write RESTful services.

To write PingResource, we need to enable JAX-RS in our application. Create an extension class for javax.ws.rs.core.

package com.sharemylocation.rest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/api/v1")
public class RestInitializer extends Application {

}

Finally, we write a pingResource that responds to/API /v1/ping requests.

package com.sharemylocation.rest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/ping") public class PingResource { @GET @Produces(value = "application/json") public String ping() { return "{'ping': 'pong'}"; }}

After submitting and publishing, we can see the results:

PingResource Demo

That’s all for today. Keep giving back.


Day 28: OpenShift Eclipse Integration for Java Developers translated SegmentFault