spring-boot

As Spring continues to evolve, it’s capabilities become more and more attractive to use. Checkout this great article by David Parish on using Spring Boot with WebSphere!

Spring Boot is a wonderful tool for creating rich powerful applications with a limited amount of code or complexity. It can optionally include Spring Data with JPA support. The Spring abstraction layer for JPA makes creating database independent applications a snap. The problem is that it uses JPA 2.1 features which WebSphere 8.5.5 does not support.

Comment and share

Fotolia_81329697_XS_T.png

Some Background

WebSphere controls whether applications (EARs and WARs) are in PARENT_FIRST or PARENT_LAST classloading mode by its deployment.xml. RAD, or Rational Application Developer, has tools to generate this, as does the application server itself, but the Eclipse plugins do not at the time of this writing. So how do you configure your applications?

Wait, what does the classloading mode do?

Simply, when your application attempts to load a class, say IOUtils from Apache Commons IO, the JVM needs to know what to do when it finds multiple classes with the same fully qualified name. You could have classes provided by your application server, global JARs that you have put into your application server, and JARs in your application.

_PARENT_FIRST _classloading means take classes found in WebSphere (the parent), or global JARs first. So if you bundle a version of Apache Commons IO that doesn’t match the parent version, your application will run with the parent version instead of your application’s version.

_PARENTLAST classloading means prefer your application’s first.

So what’s the problem?

Imagine this scenario:

  • Your servers have global JARs setup that specific applications running in the same WebSphere profile require
  • You start a new application in your local development environment and you don’t realize the server has overridden certain JARs
  • You bundle newer versions of those JARs in your application
    So, with the “It works on my machine” stamp of approval you push to your servers only to find… failure.

There are plenty of other scenarios that could lead to an issue as well.

What’s the silver bullet?!

Unfortunately, there is no silver bullet. You may want to re-evaluate having shared libraries, update their versions, change the classloading mode of your application, or use a different WebSphere profile. The right answer really depends on your environment, but if changing the classloading mode is right for you, read on!

Changing the classloading mode

There are a few ways to make this change:

  • Deploy your application to WebSphere and update the configuration in the admin console (or wsadmin)
  • Use RAD (Rational Application Developer) to change it, if you have RAD
  • Or, use a new tool that I’ve created!

Generating deployment.xml in WebSphere

To obtain a deployment.xml from WebSphere:

  1. Build your application as an EAR
  2. Manually install it to WebSphere (via your admin console at http://localhost:9060/ibm/console for example)
  3. Open your application in the admin console and configure your EAR’s classloader mode
  4. Use Manage Modules to configure the WARs’ classloader modes
  5. Save your changes to WebSphere
  6. Export your application from the admin console as an EAR
  7. Copy its ibmconfig with deployment.xml deeply nested in it

Generating deployment.xml in RAD

Generating deployment.xml in RAD is much easier:

  1. Right click your EAR
  2. Go to Java EE -> Open WebSphere Application Server Deployment
  3. Scroll to the bottom and configure classloader settings as desired

Comparing WebSphere’s generated deployment.xml to RAD’s

Updating the configuration in WebSphere generates a deployment.xml that you may not want to re-use. Let’s take a look at the differences:

WebSphere’s version

deployment.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version=“1.0” encoding=“UTF-8”?>
<appdeployment:Deployment xmlns:appdeployment=“http://www.ibm.com/websphere/appserver/schemas/5.0/appdeployment.xmi“ xmlns:xmi=“http://www.omg.org/XMI“ xmi:version=“2.0” xmi:id=“Deployment_1428026415557”>
<deployedObject xmi:type=“appdeployment:ApplicationDeployment” xmi:id=“ApplicationDeployment_1428026415557” deploymentId=“0” startingWeight=“1” binariesURL=“$(APP_INSTALL_ROOT)/WIN-F8QH15674T8Node02Cell/JpaExample.ear” useMetadataFromBinaries=“false” enableDistribution=“true” createMBeansForResources=“true” reloadEnabled=“false” appContextIDForSecurity=“href:WIN-F8QH15674T8Node02Cell/JpaExample” filePermission=“..dll=755#..so=755#..a=755#..sl=755” allowDispatchRemoteInclude=“false” allowServiceRemoteInclude=“false” asyncRequestDispatchType=“DISABLED” standaloneModule=“false” enableClientModule=“false”>
<targetMappings xmi:id=“DeploymentTargetMapping_1428026415557” enable=“true” target=“ServerTarget_1428026415557” />
<modules xmi:type=“appdeployment:WebModuleDeployment” xmi:id=“WebModuleDeployment_1428026415557” deploymentId=“1” startingWeight=“10000” uri=“JpaExampleWeb.war” classloaderMode=“PARENT_LAST” containsEJBContent=“0”>
<targetMappings xmi:id=“DeploymentTargetMapping_1428026415558” target=“ServerTarget_1428026415557” />
<classloader xmi:id=“Classloader_1428026415558” />
</modules>
<classloader xmi:id=“Classloader_1428026415557” mode=“PARENT_LAST” />
<properties xmi:id=“Property_1428026415557” name=“metadata.complete” value=“false” />
</deployedObject>
<deploymentTargets xmi:type=“appdeployment:ServerTarget” xmi:id=“ServerTarget_1428026415557” name=“server1” nodeName=“WIN-F8QH15674T8Node02” />
</appdeployment:Deployment>

RAD’s version:

deployment.xml
1
2
3
4
5
6
7
<?xml version=“1.0” encoding=“UTF-8”?>
<appdeployment:Deployment xmlns:appdeployment=“http://www.ibm.com/websphere/appserver/schemas/5.0/appdeployment.xmi“ xmlns:xmi=“http://www.omg.org/XMI“ xmi:version=“2.0” xmi:id=“Deployment_1423511656329”>
<deployedObject xmi:type=“appdeployment:ApplicationDeployment” xmi:id=“ApplicationDeployment_1423511656329” startingWeight=“10”>
<modules xmi:type=“appdeployment:WebModuleDeployment” xmi:id=“WebModuleDeployment_1423511656329” startingWeight=“10000” uri=“JpaExample.war” classloaderMode=“PARENT_LAST” />
<classloader xmi:id=“Classloader_1423511656329” mode=“PARENT_LAST” />
</deployedObject>
</appdeployment:Deployment>

The differences

Primarily, the WebSphere version includes node/cell specifics. If you take that version and deploy to a different server, you will end up with new folders in your profile/installedApps directory. This doesn’t cause any issues (that I’ve experienced) but its annoying.

A better solution for non-RAD users

If you take a look at the RAD version, its pretty basic and just lists the applications deployed and which classloading mode they are in. The ids are a standard format with the time after them.

So, assuming you are not using anything else in deployment.xml, this file is easy to reproduce! And to make it easier, I created a command line application to do it for you!

https://github.com/craigstjean/WebSphere-Deployment-Xml-Tool

To use it, invoke the JAR, passing it your application.xml as the first parameter, followed by either:

  • Nothing (which shows you its menu)
  • list (lists the EAR and all web application’s classloading mode)
  • set parent first (sets the EAR and all web applications to PARENT_FIRST)
  • set parent last (sets the EAR and all web applications to PARENT_LAST)
    For example:
1
java -jar WebSphere-Deployment-Xml-Tool.jar C:\workspace\MyProject\MyEar\META-INF\application.xml set parent last

And that’s it!

If you have any troubles with it, please create a GitHub issue and I’ll take a look!

Comment and share

Cloud security

Some Background

I keep a private development server (on DigitalOcean) running FreeBSD. None of the users have passwords, I only use certificate based authentication to get in.

Ever since I first heard of Duo (https://www.duosecurity.com/product) I’ve wanted to integrate it into some of my projects. I figured hey, why not start with my dev server!

Getting Started

Honestly, Duo has done an amazing job at making the process extremely easy.

First, create your free account at https://signup.duosecurity.com/.

In the process, you will want to download their app to your phone (if you want to use push notifications, otherwise you can use SMS).

After you have followed the wizard, you are ready to integrate it into your system, your application, or whatever else you want.

Integrating Duo with FreeBSD

After signing into the Duo administrative dashboard, simply create a new Integration and choose UNIX Integration. Then, at the top of the page click the Duo Unix documentation and follow the guide. Consistent with everything Duo has done so far, it is both thorough and clear.

I chose only to integrate Duo with SSH, not with PAM. So for me I simply had to:

  1. Download duo_unix (https://dl.duosecurity.com/duo_unix-latest.tar.gz)
  2. Compile and install it
  3. Configure /etc/duo/login_duo.conf with the keys generated on your integration page
  4. Execute /usr/sbin/login_duo to test the configuration
  5. Add this to my sshd_config:
    ForceCommand /usr/sbin/login_duo

    That’s it! Now I login to my server with my SSH key, approve the login on my phone, and I’m good to go!

I’ve chosen to configure login_duo.conf to automatically choose push notifications (as opposed to SMS or phone call), and also configured it to only be enabled from the wheel group. For reference, here is my configuration:

[duo]
; Duo integration key
ikey = ***
; Duo secret key
skey = ***
; Duo API host
host = ***
; Send command for Duo Push authentication
pushinfo = yes
group = wheel
autopush = yes
prompts = 1

What’s Next?

Next, I might choose to integrate Duo with PAM (documented in the same integration guide), add it to WordPress, add it to my own applications, who knows?! Either way, I’m certainly impressed with what Duo has come up with.

Comment and share

Fotolia_69777853_XS-300x225.jpg

Getting Spring and Hibernate to place nice with WebSphere via JPA is an adventure in trial and error. In this post, I will guide you through what you need to make it work!

Just want the code? Head over to my GitHub at https://github.com/craigstjean/WebSphere-JPA-Spring-Hibernate

WebSphere 8.5 Users

Before we get started: WebSphere 8.5 supports JPA 2.0, not JPA 2.1. Be careful not to grab Hibernate JARs for JPA 2.1.

Obtaining Dependencies and Initial Setup

First thing’s first, I grabbed the following dependencies:

  • Hibernate 4.2.17.Final

    • lib/jpa/hibernate-entitymanager-4.2.17.Final.jar
    • lib/required/antlr-2.7.7.jar
    • lib/required/dom4j-1.6.1.jar
    • lib/required/hibernate-commons-annotations-4.0.2.Final.jar
    • lib/required/hibernate-core-4.2.17.Final.jar
    • lib/required/javassist-3.18.1-GA.jar
    • lib/required/jboss-logging-3.1.0.GA.jar
    • WARNING: Do not take the following JARs, they conflict with the APIs built in to WebSphere:

      *   lib/required/hibernate-jpa-2.0-api-1.0.1.Final.jar
      
      • lib/required/jboss-transaction-api_1.1_spec-1.0.1.Final.jar
  • Spring 4.1.6.RELEASE

    • aopalliance-1.0.jar
    • commons-codec-1.10.jar
    • spring-aop-4.1.6.RELEASE.jar
    • spring-beans-4.1.6.RELEASE.jar
    • spring-context-4.1.6.RELEASE.jar
    • spring-context-support-4.1.6.RELEASE.jar
    • spring-core-4.1.6.RELEASE.jar
    • spring-expression-4.1.6.RELEASE.jar
    • spring-instrument-4.1.6.RELEASE.jar
    • spring-jdbc-4.1.6.RELEASE.jar
    • spring-orm-4.1.6.RELEASE.jar
    • spring-tx-4.1.6.RELEASE.jar
    • spring-web-4.1.6.RELEASE.jar
    • spring-webmvc-4.1.6.RELEASE.jar
  • slf4j (Logging)

    • slf4j-api-1.7.12.jar
    • slf4j-simple-1.7.12.jar (just for the example)
    • jcl-over-slf4j-1.7.12.jar
      Additionally, I setup my application to use PARENT_LAST classloading. This is just something I have gotten used to, so if you have difficulties getting your code to work and you are not in PARENT_LAST, give that a try.

Setting up web.xml

First, add the following for Spring:

    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/spring/application-context.xml</param-value>
    </context-param>

    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
       <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <servlet>
       <servlet-name>dispatcher</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
       <servlet-name>dispatcher</servlet-name>
       <url-pattern>/</url-pattern>
    </servlet-mapping>
Next, I have my datasource bound to jdbc/ExampleDb, and my Persistence Unit name will be Example Unit, so I added:
    <resource-ref>
       <res-ref-name>jdbc/ExampleDb</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
       <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>

    <persistence-unit-ref>
       <persistence-unit-ref-name>persistence/ExampleUnit</persistence-unit-ref-name>
       <persistence-unit-name>ExampleUnit</persistence-unit-name>
    </persistence-unit-ref>

ibm-web-bnd.xml

Don’t forget to bind your data source resource in ibm-web-bnd.xml:

<resource-ref name="jdbc/ExampleDb" binding-name="jdbc/ExampleDb" />

Spring Configuration

The servlet-context.xml has nothing special in it for this example, but the application-context.xml does. It points Spring to the WebSphere UOW Transaction Manager, and references the persistence unit to use:

    <bean id="entityManagerFactory" class="javax.persistence.Persistence" factory-method="createEntityManagerFactory">
        <constructor-arg type="java.lang.String" value="ExampleUnit" />
    </bean>

    <bean id="transactionManager"
        class="org.springframework.transaction.jta.WebSphereUowTransactionManager" />
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />

persistence.xml

And lastly, the persistence.xml:

<?xml version=”1.0” encoding=”UTF-8”?>
<persistence xmlns=”http://java.sun.com/xml/ns/persistence
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd
version=”2.0”>
<persistence-unit name=”ExampleUnit” transaction-type=”JTA”>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:comp/env/jdbc/ExampleDb</jta-data-source>
<properties>
<property name=”hibernate.dialect” value=”org.hibernate.dialect.PostgreSQLDialect” />
<property name=”hibernate.transaction.factory_class” value=”org.hibernate.transaction.CMTTransactionFactory” />
<property name=”hibernate.transaction.jta.platform” value=”org.hibernate.service.jta.platform.internal.WebSphereExtendedJtaPlatform” />
<property name=”hibernate.transaction.flush_before_completion” value=”true” />
<property name=”hibernate.transaction.auto_close_session” value=”true” />
<property name=”hibernate.temp.use_jdbc_metadata_defaults” value=”false” />
<property name=”hibernate.show_sql” value=”false” />
<property name=”hibernate.query.substitutions” value=”true ‘Y’, false ‘N’” />
<property name=”hibernate.cache.use_second_level_cache” value=”true” />
</properties>
</persistence-unit>
</persistence>

Here we are doing the following:

  • Setting transaction-type to JTA (Java Transaction API)
  • Setting the JPA Provider to Hibernate
  • Setting the data source to our web component’s ExampleDb
  • Setting Hibernate to use the PostgreSQL dialect
  • Setting Hibernate to use the CMTTransactionFactory for container managed transactions
  • Setting Hibernate to use Webphere’s JTA Platform
  • And so on
    Note that I did have an issue with Hibernate on startup trying to determine type information, which is what hibernate.temp.use_jdbc_metadata_defaults resolved for me. I did not have to set this when using Oracle. This did not used to occur with older versions of Hibernate (e.g. 4.1.9.Final).

Congratulations!

WebSphere is now running a Spring MVC web application, leveraging JPA 2.0 and Hibernate! For my full source, checkout GitHub at https://github.com/craigstjean/WebSphere-JPA-Spring-Hibernate

WebSphere 7.0 Users

I have had success using Hibernate 4.1.9.Final using the same configuration, though I was using version 1.0 in my persistence.xml. Also, you must place the Hibernate JPA API 2.0 JAR that is included with Hibernate in the lib directory. If you migrate to WebSphere 8.5, you must remove that JAR.

Comment and share

Settings button

When working in Eclipse or RAD, I immediately start modifying my environment. Here are some of the changes that I make that might improve your day, please feel free to share you own!

Stop the Servers tab from taking over!

Whenever server state changes (e.g. from Stopped to Starting, to Started, to Republish, etc), the Servers tab automatically shoves its way onto your screen because it wants you to know it did something. Great! But if you’re like me, you immediately dismiss it and now you’re a little distracted and a little annoyed. Here’s how to turn it off!

From within Eclipse’s / RAD’s preferences, click Server. Then uncheck “Show Servers view when server state changes.”

2015-03-30 23_31_39-Preferences

Stop the Console tab from taking over!

Now that we stopped the Servers tab, let’s do the same thing for Console! Output is constantly written to the console, and I typically look at it when I want it, and otherwise don’t care. So, go to preferences, expand Run/Debug, and click Console. Then uncheck “Show when program writes to standard out,” and “Show when program writes to standard error.”

2015-03-30 23_31_46-Preferences

Where did my Console output go? And why can’t I scroll up?!

The Console view defaults to limiting its output. This may make it easier to work with, but has the nasty side-effect of dropping important content when you have large stack traces, or multiple stack traces. To turn it off, go to the Console preferences (from above) and uncheck “Limit console output”.

Makes your static imports work in autocomplete! (e.g. JUnit, Mockito, etc.)

UNIT TESTING! Ok, ok, let’s calm down a little. If you write a lot of tests, and you use JUnit, Mockito, EasyMock, etc., or if you happen to use a library that leverages static imports, you are probably tired of manually adding them to your imports list. But, _you don’t have to! _Go to the preferences, expand Java, then Editor, then Content Assist and click Favorites. Then, click “New Type” and type in org.junit.Assert. Eclipse will add org.junit.Assert.* to the favorites list, and now when you are writing your unit tests you can start typing assertEq, hit Control + Space, and it will autocomplete the static import and add it to your class!

2015-03-30 23_36_04-Preferences

Other, more personal editor settings.

These settings are definitely up to personal preference, but in case you want to give them a try here are some other changes I make:

Use Spaces, not Tabs

You should be using Spaces and never tabs**! **_I’m kidding!!! _Since I have no interest in arguing my personal preferences, here is how you make the change in case you want to: open preferences, expand General, expand Editors, and click Text Editors. Then, check “Insert spaces for tabs” (or don’t).

2015-03-30 23_31_07-Preferences

Auto format and auto organize imports on save

Why should I have to worry about constantly re-indenting my code, fixing it up, etc.? My time is better spent doing other things, you know like writing code instead of styling it. So, I set my formatter preferences, then go to preferences, expand Java, then Editor, then click Save Actions. Then I check “Perform the selected actions on save,” “Format source code,” and “Organize imports.” My personal feeling towards this is you will feel much more free if you make the change, if you can live with the formatter settings you choose. Give it a try!

2015-03-30 23_31_24-Preferences

What changes do you make?

Comment and share

I have been using WebSphere Application Server since 2006, and through that time I have had a blast sharing what I’ve learned with other developers who could benefit from it. So, finding inspiration to put together this blog wasn’t difficult, I only needed a little push and direction.

Then comes John Sonmez (http://simpleprogrammer.com). I had the great opportunity of meeting John at the Pluralsight Author Summit earlier this month and what I found was someone with a passion for helping others. Then I saw he has a free course on how to properly start blogging (at http://devcareerboost.com/blog-course/). After signing up, you get e-mails from John with instructions, guidance, and even homework to keep you on task.

So please, enjoy the content that I put on my site. And if you would like to start your own blog, check out John’s free course!

Comment and share

Fotolia_62142043_XS-300x228.jpg

Did you know you can run WebSphere Application Server in development mode, which optimizes it for less powerful hardware and for applications which see frequent updates? It’s actually quite easy, whether you are creating a new profile or working with an exciting one!

Creating a New Profile with the Profile Management Tool

If you are creating a new profile, via the Profile Management Tool (or PMT), just make sure you select these two boxes:

  1. Use the “Advanced profile creation” mode
    2015-03-20 10_57_44-Profile Management Tool 8.5
    This allows you to tweak some very simple settings that you may be interested in, I recommend all people using PMT to create their profiles in this mode.
  2. Switch “Server runtime performance tuning setting” to “Development”
    2015-03-20 11_03_18-Profile Management Tool 8.5
  3. Then just finish the wizard with the settings you want!

Creating a New Profile via the Command Line manageprofiles Tool

For those command-line junkies like myself, creating a new profile via manageprofiles in development is also extremely easy. Simply add this flag to your -create parameter list:

-isDeveloperServer

That’s it! To read more about the parameters for manageprofiles, see here.

Modifying a Profile via the WebSphere Admin Console

If you’ve already created your profile, and want to simply enable development mode, simply log into the admin console and change it! It goes like this:

  1. Go to the WebSphere Administrative Console (e.g. http://localhost:9060/ibm/console)
  2. Log in

    1. Tip: If you do not have security enabled, just click Log In
  3. Drill into WebSphere application servers
    2015-03-20 11_08_46-WebSphere Integrated Solutions Console

  4. Drill into your server (e.g. server1)
    2015-03-20 11_09_01-WebSphere Integrated Solutions Console
  5. And check the following boxes:

    1. Run in development mode
    2. Parallel start
    3. Start components as needed
      2015-03-20 11_09_09-WebSphere Integrated Solutions Console
  6. Then click Save, and restart your server!

Modifying a Profile via the Command Line wsadmin Tool

Enabling development mode on an existing server is the most complicated choice, but luckily it is still pretty straight forward. Just follow these steps:

  1. Start your server (via Eclipse/RAD/RSA/Other IDE, or by command line)

    1. To start by command line, open your command prompt
    2. cd into your profile’s bin directory (e.g. C:\Profiles\MyAwesomeProfile\bin)
    3. startServer.bat server1 (or, ./startServer.sh server1 if you are not on Windows)
  2. With the command prompt in the bin directory (from the above steps), execute the following:

    wsadmin.bat server1
    set server [$AdminConfig getid /Server:server1/]
    $AdminConfig modify $server "{developmentMode true}"
    $AdminConfig modify $server "{parallelStartEnabled true}"
    $AdminConfig save
    quit
  3. Then, just restart your server!

    1. Tip: You can use stopServer.bat server1 to do this too (or ./stopServer.sh server1)
      Enjoy!

Comment and share

  • page 1 of 1

Craig St. Jean

Father, programmer, constant learner, @pluralsight author


Software Architect