Posts Tagged ‘Java’
Are You Learning Java? Why?
The other day, I began thinking about how to learn to become a programmer/software developer. Then I began thinking about learning C, C++, Java, Groovy, Ruby, Scala, PHP, Coldfusion, Perl, Python JavaScript and so on. I started thinking about beginning programmers because my youngest brother has started learning C in school. He has started with the standard:
#include<stdio.h>
main()
{
printf("Hello World");
}
I started programming in a similar fashion and then I would eventually begin to learn PHP then Coldfusion, Java, Groovy, Ruby, Python…I was wondering what sparked my interest to learn other languages. My biggest effort or concentration has been learning Java, that is what I do 9-5 and I even became a SCJP, why you ask? Good question. What if I was a COBOL programmer or RPG programmer, why would I start to learn Java? Here are some reasons I came up with:
* I am in school/fresh out of school and I want to learn Java because:
- I took an intro to programming class and the professor/other students were talking about Java.
- I have a few friends that are working for big companies that are learning Java.
- I see a lot of Java jobs advertised on the job sites.
- I have been working with Web scripting languages but I would like to see the Java approach.
* I am a COBOL, RPG or legacy programmer and I want to learn Java because:
- My company is re-platforming legacy applications and moving to Java.
- I see a lot of Java jobs advertised on the job sites.
- Java developers at my company make more money.
- I want to be able to put Java on my resume and become more marketable.
Maybe you have some other reasons to learn Java?
Groovy Prime Numbers
The other day I wanted to prove the power of Groovy to a few more core Java developers. I sat down and played with a little script that I think proves the power, or ease of use, of Groovy while having fun with number theory. My goal was to have a Collection that contains Prime Numbers calculated from a given range of numbers x to y, or in Groovy syntax x..y. I am taking a few things for granted with this script. For example, I know that 2 is the lowest Prime Number (it helps simplify the algorithm) and that 1 is not a Prime Number. So here’s the script:
def t = 1..100
def v = []
t.each { n ->
(2..n).each { d ->
if(n % d == 0 && n != d)
v.add(n)
}
}
println t - v - 1
We have our range of numbers t and we are capturing the non-Prime numbers and adding them to our Collection v. The final line of the script is doing a lot of groovy work for us that would take a few more lines of code with plain Java syntax. What is it doing for us? It is taking our range of numbers t and subtracting (removing) our non-Prime numbers collected in v. This line is also removing the number 1 because we know that 1 is not a Prime Number. Finally, it is printing the result like so:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
Very cool stuff. Now, I know that many of us are rarely building a Collection of Prime Numbers and I know this script does not do things in a timely manner with a range like 1..10000 (it took a couple of minutes), but I am sure this feature of Collections in Groovy can be utilized in many ways in my development.
IBM and Sun Microsystems
Although many people believe that IBM and Sun Microsystems are synonymous with Java, especially in the corporate environment, I do believe that there is a need for keeping things separate. The concept is somewhat similar to the separation of church and state here in the United States. On one side, we have the belief and ability to make changes and foster ideas to keep things open and less constrained. Then on the other side, we have walls and boundaries that limit when and where we can go. We don’t have to mix our beliefs with every aspect of what we do and we surely do not want our limitations to constrain our ideology and options. I appreciate what each company does within their own space but I am a bit weary about IBM purchasing Sun.
Apache CXF: Open Source Service Framework – The Client
Earlier this year I posted Apache CXF: Open Source Service Framework to demonstrate how easy it is to create a simple echo Web Service with Apache CXF. Now I would like to post an example client for that service using Apache CXF.
First, we need our dependencies: (download them here)
... commons-logging-1.1.1.jar cxf-2.1.2.jar FastInfoset-1.2.2.jar geronimo-activation_1.1_spec-1.0.2.jar geronimo-annotation_1.0_spec-1.1.1.jar geronimo-javamail_1.4_spec-1.3.jar geronimo-jaxws_2.1_spec-1.0.jar geronimo-servlet_2.5_spec-1.2.jar geronimo-stax-api_1.0_spec-1.0.1.jar geronimo-ws-metadata_2.0_spec-1.1.2.jar jaxb-api-2.1.jar jaxb-impl-2.1.7.jar jaxen-1.1.jar jdom-1.0.jar jetty-6.1.9.jar jetty-util-6.1.9.jar neethi-2.0.4.jar saaj-api-1.3.jar saaj-impl-1.3.jar spring-beans-2.0.8.jar spring-context-2.0.8.jar spring-core-2.0.8.jar spring-web-2.0.8.jar stax-utils-20060502.jar wsdl4j-1.6.2.jar wstx-asl-3.2.4.jar xml-resolver-1.2.jar XmlSchema-1.4.2.jar ...
Then we need the Echo Service interface (this should be provided for us from our service providers) :
...
package com.thejavajar.service;
import javax.jws.WebService;
@WebService
public interface EchoServiceIfc {
public String echo(String text);
}
...
The last piece of the pie is our client that makes the call out to http://localhost:9000/echo, where our service lives, in order to invoke an operation on the service.
...
package com.thejavajar.client;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import com.thejavajar.service.EchoServiceIfc;
public class EchoClient {
public static void main(String[] args) {
String request = new String("test 1");
ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
factory.setServiceClass(EchoServiceIfc.class);
factory.setAddress("http://localhost:9000/echo");
EchoServiceIfc client = (EchoServiceIfc) factory.create();
System.out.println("Sending: " + request);
String response = client.echo(request);
System.out.println("Returned: " + response);
System.exit(0);
}
}
...
All we need to do is run our main method in our EchoClient class and we will get something like:
... Sending: test 1 Returned: test 1 ...
That’s all folks. Obviously, this example, along with our previously posted service, is not solving complex problems that we might face in our current Java development endeavors but it provides us with peak into Apache CXF and opens up more people to a technology that I enjoy working with.
Eclipse Project: apache-cxf-echo-client.zip