Murphy Bytes

August 16, 2011

Consume SOAP Webservices the Easy Way with JAX-WS and JRuby

Filed under: Uncategorized — John Murphy @ 7:42 pm

SOAP has a bad reputation in the Ruby community. Most Rubyists prefer Restful services because they are a lot easier to consume and are well supported in tools like Rails. However, there are times where you have to consume a SOAP based web service. In this article, I’ll show you how you can easily consume a SOAP client using JRuby and JAX-WS. The source code for this exercise can be found here.

First, I’m going to grab the wsdl that defines the service we want to consume. Then I’m going to use the JAX-WS wsimport utility to turn the wsdl into java code. Then I’ll compile the Java code and create a jar that I will then use in my ruby code to interact with the web service.

Requirements

You’ll need to install a couple of things to get started. These include the following:

I’d also suggest RVM but that is optional. If you want to build something based on my example you’ll want to clone my git repository.


> git clone ssh://git@github.com/murphybytes/SampleSoapClient.git
> cd SampleSoapClient
> bundle install

So now you have your environment set up. What next? Let’s think a bit about how we want to structure our project. We are consuming the web service so lets make that a sub project of our main project. We’ll put the web service artifacts in SampleSoapClient/Webservice So we end up with something like this.


SampleSoapClient/lib
SampleSoapClient/spec
SampleSoapClient/script
SampleSoapClient/Webservice/generated
SampleSoapClient/Webservice/test
SampleSoapClient/Webservice/wsdl

I like to include the wsdl files defining the service in my project. With the wsdl files in place I’ll need to write an ant build script that will turn my wsdl into a jar that I can use in my Ruby code. Creating the build.xml is probably the hardest part of the project. Once you create your build script, you can use it as the template for the script to consume any SOAP based web service. Here’s mine.


<project name="OrderHistoryServiceAdapter" basedir="." default="jar">
  <taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport">
    <classpath path="lib/jaxws-tools.jar" />
  </taskdef>
  <target name="clean">
    <delete includeemptydirs="true" failonerror="false" >
      <fileset dir="bin" includes="**/*" />
      <fileset dir="generated" includes="**/*" />
      <fileset dir="dist" />
    </delete>
  </target>

  <target name="compile" depends="clean">
    <wsimport
wsdl="wsdl/OrdersHistoryService.wsdl"
destdir="bin"
sourcedestdir="generated"
xadditionalHeaders="true"
/>
    <javac destdir="bin" >
      <src path="test" />
      <src path="src" />
      <classpath path="lib/junit-4.9b3.jar" />
    </javac>
  </target>

  <target name="jar" depends="compile">
    <mkdir dir="dist" />
    <jar destfile="dist/OrdersHistoryService.jar" basedir="bin" />
  </target>

  <target name="test" depends="jar" >
    <junit printsummary="true" showoutput="true" >
      <classpath>
<pathelement path="dist/OrdersHistoryService.jar" />
<pathelement path="lib/junit-4.9b3.jar" />
      </classpath>
      <formatter type="plain" />
      <test name="com.eris.orderhistory.test.SessionTokenManagerTest" />
    </junit>
  </target>
</project>

Now, using JRuby, we can interact with our web service using pure Ruby code. All we have to do is add the requisite requires and import the classes that we want to use. Here’s an example.


# required to import native java
require 'java'
# the jar that implements our web service
require 'WebServiceClient/dist/OrdersHistoryService.jar'

# import native java classes
java_import com.currenex.webservice.definitions.AuthenticationPortType
java_import com.currenex.webservice.definitions.OrdersHistoryPortType

That’s all there is to it!

April 21, 2011

Down to the Metal

Filed under: Uncategorized — John Murphy @ 9:11 pm

I just spent a few days over at Pedal Brain programming (or trying to program) an MSP 430 Micro Controller. Embedded is hard but I think it’s worth trying if you are a coder. It’s kind of the essence of coding. Unlike most of the programming I’ve done, in embedded there is nothing between you and the machine. Typically you don’t have a lot of memory or CPU so you have to be very efficient with resources. I think in the long run the economy you pick up in the embedded realm makes you a better coder in other areas.

December 25, 2010

Kinney/Murphy Annual Holiday Message

Filed under: Uncategorized — John Murphy @ 7:33 pm

It’s Christmas Eve and we’re hanging out at home waiting for Santa. It’s been a busy year for our family. This was our first full year in our new house and Plymouth is now starting to feel like home. We love our neighborhood. Sam has other little boys nearby to play with and Lisa and I love our neighbors. We were able to take a family vacation to Disney World this year and Lisa and I went to NYC in June. We were thinking that our first year in our new house would be our last as my employer, Dow Jones, was asking me to relocate to Princeton, NJ. However, the relocation was canceled and it looks like we’ll be staying in Minneapolis for the time being. It’s going to be a white Christmas, 36 inches of snow already this year. I don’t need to go to the gym, I get all the exercise I need shoveling tons of snow out of the driveway.

Santa Meeting 2010

P1030005.JPG
P1030455-adjust.JPG
photo.JPG
P1030338.JPG

Sam

This has been a big year for Sam. He learned to swim and got a green belt in Karate. Last year, Santa brought Sam an Australian Shepherd puppy named Pete. We all liked Pete so much we got him a little pal, another Aussie pup named Gus. It’s pretty chaotic around our house with two Aussie pups and an energetic six year old running around. Samuel started Chinese Immersion School in Excelsior this year. He loves school and is doing great. We love the school too. We’d heard a lot in the media about how bad public schools are so we were concerned when Sam was ready to start school but Mrs. Tao and the Excelsior school is amazing. We get email status updates on a regular basis and are able to dialog with the teacher whenever it is needed. Sam is learning so much. I observed his class one day last month and the whole class was conducted in Chinese. We don’t want Sam to get behind in his English curricula so we work hard at home to help Sam with reading and arithmetic. Sam says 圣诞节快乐 to everyone.

Audrey

Audrey enjoys posting humiliating pictures of me on Facebook. Luckily for her, I won’t stoop to the same behavior on my blog, it wouldn’t be…. Christmas like. Audrey has had a lot of great opportunities and experiences this year. She recently started a new job at CF Moto where she is moving into marketing. She also shot a PSA commercial in Los Angelos and will be on a TV show that has something to with hair salons. She also has taken to attending Ruby Users of Minnesota (RUM) with me. I don’t know why. Audrey says ‘because it’s awesome’ but I think she likes to walk around in heels and distract the rest of the geeks there.

Hannah

Hannah is living in Ames. She’s been gaining experience as a waitress this year and hoping to find a better job. She and Lisa made a trip to Conifer Colorado to pick up the newest member of our family, Gus the dog.

Lisa

Lisa finally admitted that she likes it here. She has been taking advantage of many of the wonderful shopping and educational opportunities that we enjoy here in the Twin Cities, learning how to make stained glass and currently learning how to knit. Lisa brought her job with Principal with us, and has been working from home. This has provided welcome income for us, but unfortunately Principal is getting out of the health care business and Lisa will be losing her job along with 1500 others. She will be done second quarter 2011. On the bright side, getting a few months severance just as the weather starts to warm up around here could make for a nice summer.

John

It’s been a great year for me. As always, I’m grateful for fabulous Lisa and my new family. It’s never a dull moment around here. Sam keeps me busy and Audrey is always an entertaining companion. I’ve been able to see a lot more of my family this year and it’s great to spend more time with my brothers and sisters. I love you all.

November 10, 2010

Sick of Virii? Get Linux.

Filed under: Uncategorized — John Murphy @ 10:17 am

Recently I’ve noticed a rash of Facebook based attempts to get you to install malware on your system. These strategies masquerade as a message from one of your friends that try to get you to click on a link that will then run a program on your system. Do you want to know a sure fire way to avoid these exploits? Don’t run Windows, use Linux instead. I know some people will think that you have to be some sort of tech guru to use Linux or they may not even know what Linux is. For these folks I’ll say that Linux is easy to use. It has lots of free software that let you create documents, browse the web, and do pretty much anything that you commonly do on your Windows system and, most importantly, it’s immune to pretty much all of the bad things that hackers perpetrate on unsuspecting Windows users. No anti virus software needed! Here are some links for various Linux distributions to find out more:

Feel free to contact me if you have questions.

September 29, 2010

GAE is Better than EC2

Filed under: Uncategorized — John Murphy @ 11:25 am

Recently, my friend Josh got me interested in Google Application Engine. I’ve been building distributed applications for a number of years now and have always assumed that if I were deploying to a cloud platform, it would be on EC2 because of choice of operating systems, and support for environments that I know well like Rails. GAE doesn’t support the same environments (at least not out of the box). It also abstracts the OS away from you almost entirely. That is the reason that I think GAE is better. I like the idea of not dealing with the OS at all. OS is complicated. To do a Unix application right you need to understand shell scripting, daemons and security. I usually like to set up a user with limited privileges for my applications to run under (no login, limited file system access, etc.) I also like to have a group for users who can admin the application and another group for a more limited set of users who can admin the machine. This of course implies that you set up sudoers correctly so an application admin can start the application as root (the app is then demoted to run as a user with limited privileges after it’s gotten access to all resources it needs to run). And of course you need to be cognizant of things you want the application administrators to have access to like read access to /var/logs. Anyway, it’s complicated to get right. GAE abstracts the OS away from you so you don’t have to worry about any of this stuff which is a good thing. You just focus on the application logic and let Google worry about the OS which is a big win in my opinion.

September 24, 2010

λ + concurrency = evil

Filed under: Uncategorized — John Murphy @ 8:52 pm

I ran into an awful bug in some JavaScript the other day that I thought I’d share as one more reason that non-functional programming languages are really awful in concurrent or asynchronous execution environments. Without further ado, here’s the code:


function update_watchlist() {

    var selected_instruments = $('div#instruments').data( 'selected_instruments');

    for( var selected =  0; selected < selected_instruments.length; ++selected ) {
    
        $.post( '/getquote',
            selected_instruments[selected],
            function( quote ) {
                $('div#quote_last' + selected ).html( quote.last );
                $('div#quote_volume' + selected).html( quote.volume );
                $('div#quote_datetime' + selected).html( quote.datetime );
            },
            'json'
            );    
    }
  
}

This code is supposed to use JQuery and ajax to update a watch list of quotes. Data values for each quote are stored in numbered divs where the div number matches the index in selected_instruments which is an array of information needed to look up a particular quote. In other words we update div id=’quote_last0′ with a quote that we look up using information in selected_instruments[0]. Can you spot the problem?

What is the value of selected in the lambda function passed to $.post? We don’t really know because post is an asynchronous call, the lamda function


            function( quote ) {
                $('div#quote_last' + selected ).html( quote.last );
                $('div#quote_volume' + selected).html( quote.volume );
                $('div#quote_datetime' + selected).html( quote.datetime );
            },

is only called after the http post to the remote server returns. By the time this has occurred, selected has passed out of scope. The fix for this problem would have been to make lambda functions in JavaScript more like first class functions so in this case, the only way to pass the index data to the lambda function illustrated above would be to pass it in the post data, and then have the server return it in the argument passed in the lambda function as illustrated below.


function update_watchlist() {

    var selected_instruments = $('div#instruments').data( 'selected_instruments');

    for( var selected =  0; selected < selected_instruments.length; ++selected ) {
    
        $.post( '/getquote',
            {
                // selected is a valid variable here
                instrument : selected_instruments[selected],
                index : selected
            },
            function( result ) {
                // result.index = selected passed to post target and returned
                $('div#quote_last' + result.index ).html( result.quote.last );
                $('div#quote_volume' + result.index).html( result.quote.volume );
                $('div#quote_datetime' + result.index).html( result.quote.datetime );
            },
            'json'
            );    
    }
  
}

September 11, 2010

Florida Fruitcake

Filed under: Uncategorized — John Murphy @ 9:51 am

The following is an excerpt from a Facebook conversation about a Gainseville, Florida preacher gaining world media attention by threatening to burn the Qur’an. I reposted it here because I think it highlights what can be great about media today. To be specific, I’m referring to how new media allows us to add depth to a particularly inane news item.

Original Post

This is a little frothy, but it gets the ball rolling. We’ll call the author ‘Bob’.

Quit giving “religion” special rights. It’s fiction. Like Duck Tales. I’m going to burn my VHS copy of Duck Tales. It’s the SAME. THING. Remember when some guy was burning Harry Potter? I’m sure some hard core fan would be shattered by that. It’s all just a really old Japanese comic book convention. Would you threaten war and curtailment of the first amendment if I burned a Pokemon comic book?

Entertaining rebuttal from ‘Sue’

Dude. I would like to posit that burning a religious book is not about “religion.”
It’s about social prejudice. Historically, societies that burn books are intellectually unsound, morally deficient, and violent. No one cares about the actual theological content of the book; it’s a symbolic act of hatred against an enemy. It’s a threat. It’s like saying, I would destroy all your people if I had the power. Book burning is the first step in a process of syndicated terror—and this is consistent across history—that leads to physical violence towards people in a specific social group, ethnicity, or culture.

“Where they burn books, they will also ultimately burn people.” –Heinrich Heine, prophetically, in a book that was ceremonially burned by Nazis in 1933.

It’s not about giving “religion” special rights—it’s about giving people the right to live without fear. To live without fear of being singled out and persecuted for their beliefs or their background. ( Like having to wear a yellow star.) Book burning is the first step. It’s just that religious books happen to make better effigies of entire cultures than comic books do. (Although I think with Duck Tales, you could support a legitimate argument for a spiritual paragon of culture…)

Bob replies…

It’s true that there’s an element of deliberate symbolism going on here, a point being shot across using a something under the surface of the book itself. And in the right situation, I definitely see the horror of the symbolism present.

I would rather see a flag burn than a book, though neither would especially blow me away. I would be more amazed by how stupid these people are than offended by what they are doing. Which I feel is a better response to these people, because it’s not the reaction they’re trying to get out of us. When this story initially broke, I just ignored it, just another crazy guy doing something crazy. I thought this was a better story about how America needs a better education system, or better access to schizophrenia drugs.

My thing is, are we going to let some nobody in Florida send the official shot down the bow? In the internet age, anybody can burn a religious book, and in minutes, anybody in the world can see it. Here’s a guy burning a bible and a koran at the same time, just now. We don’t even need to wait for the Florida guy to do anything, the deed is done: http://www.youtube.com/watch?v=MZ4rBJfcIfE

In order for us to live in the new media world, we need to stop taking crackpots seriously, or we will not be able to live cohesively. There’s probably one Japanese guy in Japan that hates America and burns flags and burgers and whatnot. Do we let that destabilize our relationship with Japan? Is he flooded with reporters? Does the Prime Minister of Japan go to his house and beg him not to do it?

If it was somebody with real authority burning books, yes I would be very worried. But we’re talking about one screwball here and his tiny little whatever-it-is. He doesn’t have real political power, or a standing army. I really don’t understand how he become the official “okay we’re going to burn books now” guy, who is “negotiating with the mosque people” and after this whole thing is over, I would bet money we’ll never hear about him or of him ever again. If people settle down and realize nothing will come of this, it probably won’t even make the history books.

‘Sue’ weighs in again…

The difference is that Jones isn’t just a random guy in Japan burning a burger, or a guy on YouTube showing how both the Bible and the Koran are made of paper and therefore ignite easily.

Those are both merely random acts of silliness, definitely not to be taken seriously, I agree. So here’s why Jones is more dangerous than a random guy:

1) Jones is an established anti-Islam, anti-Muslim, anti-Arab author. He hates the entire religio-ethnic group and thinks they should die. And he has published books to that effect. So he’s not just a guy holding a Bic to some paper. He’s doing it out of outspokenly racist hatred.
2) American soldiers are at war in places like Afghanistan, where there is violence between religio-ethnic groups. That is very different from a guy in Japan burning a burger, right? We’re not enemies at war there. Jones believes that American Christians are at war with Muslims at large. He is sending this as a message, almost as if the book-burning were an act of war itself.
3) The burning was going to be on September 11th. That’s huge. That’s not just some guy on YouTube putting up a video of a fire. That’s an act of war. Jones sees that as the date on which Muslims declared war on Christians. If he commenced with the burning, Muslims abroad could get the impression that that’s what all Christians believe; that the two religions are opposed and hate each other. Which is NOT the case. The burning could legitimize the dynamic polarity between two sides which are not actually enemies. It could potentially be as large a catalyst as the German book-burnings of 1933, which were carried out by University students. Nobodies.

Anyway he’s already said he won’t go through with it, so you’re right, I don’t think it will make the history books in the end.

Yours truly weighs in

What a fabulous discourse. I really love how Sue ties in historical events to support her points. I have the historical perspective of having been around to experience the mass media during the sixties through today. When I was a kid, I don’t think we wouldn’t have heard of Jones. The US national news outlets were controlled by the big three networks who in turn voluntarily subscribed to certain standards of good taste (don’t remember what these were but it was the Better Broadcasting Bureau or some such). The upshot of this is that news reporting was much more fact based and the agendas of the broadcasters were expressed in what news they chose to report, not how they reported it. You saw things like real combat footage with blood and screaming (unlike sanitized war reporting of today) but not much about crackpots like Jones. Of course we did have Abbie Hoffman, but he didn’t take over the news to the degree that Jones has, plus he actually had something to say. Note that the their was a lot of coverage around the trial of the ‘Chicago 7′ but it was not about Hoffman himself even though he was extremely flamboyant and media savvy. My point is, that now there is no such control and it seems like things become significant through some sort of emergent mob mentality more than any other way. I don’t know if that is good or bad, but what’s scary to me is the degree to which people can be influenced by this and not even know that they are being influenced. It’s also disturbing to me how mainstream media focuses on sensational news that has high emotional impact to drive profits instead of news that has high impact from a economic/societal basis. I think that the upshot of all this is that there is a large cohort of educated critical thinkers are abandoning traditional media (TV/Radio) and getting their news from the internet. Unfortunately, this trend further dumbs down the audience of people who still watch mainstream media which leads them to dumb down their content and pick up increasingly sensationalistic content that is often of no real importance.

June 5, 2010

Do Be Do Be Do

Filed under: Uncategorized — John Murphy @ 11:51 am

I’ve been thinking a lot about why we do things. Not particular things mind you but things in general. Why do anything at all? Why think about why we do things? One might conclude that it’s inevitable that I write about being at this particular time and place if you believe as I do that free will is largely illusory. More and more contemporary research* seems to indicate we are nothing more than sophisticated genetically programmed meat machines with an incredible variety of possible execution paths. Note that I don’t choose the particular path I’m on. It would be more appropriate to say that I’m nudged down one branch or another by a combination of external stimuli and instinct. In the past I have sometimes become fatalistic when pursuing such lines of reasoning ( one can also adopt a hedonistic stance for obvious reasons ). I’m not fatalistic any more. I am constrained by a certain set of possibilities and my acting takes me down a path that will eventually end at the same outcome as it does for every other human on this planet. The conclusion I arrive at is that I am here and I will do things and it is the doing that is the reason for being and vis versa. If I’m going to do things I might as well enjoy what I’m doing. If I’m going to be, I might as well choose to have a positive attitude about what being will be like in the future. If I do I’m happier in the moment which seems to be a reasonable thing to strive for. But wait. Don’t I decide to enjoy what I’m doing? Don’t I decide to adopt a certain attitude about the future? Doesn’t that contradict what I was just saying? It’s pretty easy to experience in the mental equivalent of a dog chasing his tail thinking in this way. My meat machine is about to experience a stack overflow so it’s time that I quit woolgathering and go on and do something else.

*I love Wikipedia
*I give money to Wikipedia to express my love thereof, if you love Wikipedia, you should too.

November 6, 2009

Learning to use the Internet

Filed under: Uncategorized — John Murphy @ 12:23 pm

I was thinking about how I’ve changed the way that I use the internet over the years and how over time I’ve gotten a lot better at using the net for what I need. Through informal interaction with other technical people and on my own I’ve been exposed to different tools, sites and applications that have made me a lot more productive than I would otherwise be. A few of the many tools I like are Delicious, Google Reader, Firebug, Firefox, and Wikipedia. The point is that it has taken me years to really learn how to be as productive as I am today and a lot of what I’ve learned has been accidental. I think that the evolution of internet has begun to stabilize a bit to the point that some best practices should be emerging in terms of its use but I wonder if school curricula are keeping up? I wonder how kids learn to use the internet? When I was a kid we had classes that taught us how to use a library to find information. It seems like today internet skills are much more important than library skills were when I was a kid. I know that there are classes kids can take on internet research but I have to wonder how relevant they are. I can’t help but think how tough and time consuming it would be to start from scratch and learn how to use the internet. In effect this is what every child has to do. How do they learn internet skills? What’s the best way to teach someone to use the internet? What things do they need to know?

February 10, 2009

UNIX Epoch time being 1234567890

Filed under: Uncategorized — John Murphy @ 5:34 pm

In the Midwest this occurs at about 5:31 PM Friday, February 13! Coincidence? I don’t think so. If you are on Facebook there is an event dedicated to this auspicious occasion.

Older Posts »

Powered by WordPress