2010年10月12日 星期二
Web Service 的幾種發佈和調用的方法
2010年10月11日 星期一
Web Service Create (Axis1)
- First, I will show the "easy to deploy" feature that lets you drop a source file into the AXIS Web application and have it become a Web service -- just like that!
- Then we will use the new WSDL2Java and Java2WSDL tools to see how we can quickly get a WSDL descriptor and access the associated Web service, and then how to easily expose some Java code.
Deploying Your Code as a Web Service in One Easy Step
- Deploy a piece of code?
- Write a client that accesses the Web service?
- Obtain the WSDL for the deployed Web service?
Working With a Production Web Service
Although it is really easy and convenient to shove our Java code under the Axis directory as a .jws file, that will not be the way you deploy all of your Web services. A lot of the time we want more fine-grained control over the Web service, to tweak it, and to use other more advanced features. Luckily, with other tools, it is still easy for us to work with our code in a more formal manner.
Let's walk through the following process:
- We have a piece of code that calculates the Fibonacci sequence for a given iteration.
- We want to take the existing code, wrap it up as a Web service, and then deploy it to the Apache Axis system.
- Once we have a running service on the server side, we will create Java stubs that allow us to communicate with the service, only requiring the WSDL.
After going through this full process, you will be able to create clients to any Web services (when given the WSDL), and wrap upany code, exposing it as a Web service.
Here are the steps we will walk through:
- View: Take a peek at the existing Fibonacci code.
- Java2WSDL: Generate the WSDL file for the given Fibonacci interface.
- WSDL2Java: Generate the server side wrapper code, and stubs for easy client access.
- FibonacciSoapBindingImpl: Fill in wrapper to call the existing Fibonacci code.
- Deploy: Deploy the service to Apache Axis.
- Client: Write a client that uses the generated stubs, to easily access the Web service.
2010年10月10日 星期日
Java Exceptions (interview 問到囉!!!)
For run time exceptions
- method signature does not need to declare runtime exceptions
- caller to a method that throws a runtime exception is not forced to catch the runtime exception
- Runtime exceptions extend from RuntimeException or Error
- method must declare each checked exception it throws
- caller to a method that throws a checked exception must either catch the exception or throw the exception itself
- Checked exceptions extend from Exception
Checked exceptions indicate an exceptional condition from which a caller can conceivably recover. Runtime exceptions indicate a programmatic error from which a caller cannot normally recover.
Checked exceptions force you to catch the exception and to do something about it. You should always catch a checked exception once you reach a point where your code can make a meaningful attempt at recovery. However, it is best not to catch runtime exceptions. Instead, you should allow runtime exceptions to bubble up to where you can see them.
Java Swing (Lightweight) vs. AWT (Heavyweight)
http://j2se.myweb.hinet.net/article/java/hw02.htm
Lightweight Component 與 Heavyweight Component
我們常會聽到許多人說 Swing 是 lightweight component,而 AWT 是 heavyweight component,指的就是 Swing是由純 Java code 所寫成,因此 Swing 解決了 Java 因 Window 類別而無法跨平台的問題,使視窗功能亦具有跨平台與延展性的特性。而且 Swing 不需佔有太多系統資源,因此我們稱 Swing 為 lightweight component,表示我們可以利用它輕易地做出各種變化。相對於 Swing,由於 AWT 的 native code 具有 C 語言的成分,若您想自行更動 AWT 的視窗變化時,您必須撰寫自己的 C 語言原生碼,然後再搭配 AWT 的原生碼與 JDK 函式庫。遇到不同的作業平台時,又必須重新修改和編譯自己所寫的原生碼。因此 AWT 不具跨平台特性、耗時且難以理解,且又耗費系統資源,所以就稱之為 heavyweight component,表示 AWT 元件是不容易更動的。而在本文第一段所提到,用 Swing 寫成的程式不保證能夠執行於所有瀏覽器上,則是單指 Java applet網頁程式而言,其肇因於微軟因商業競爭考量,其 IE 瀏覽器本身不再繼續支援 Java 程式的緣故。
2010年10月7日 星期四
Java Primitive Data Types (interview 竟然有問!!!)
boolean
1-bit. May take on the values true
and false
only.
true
and false
are defined constants of the language and are not the same as True
and False
, TRUE
and FALSE
, zero and nonzero, 1 and 0 or any other numeric value. Booleans may not be cast into any other type of variable nor may any other variable be cast into a boolean.
byte
1 signed byte (two's complement). Covers values from -128 to 127.
short
2 bytes, signed (two's complement), -32,768 to 32,767
int
4 bytes, signed (two's complement). -2,147,483,648 to 2,147,483,647. Like all numeric types ints may be cast into other numeric types (byte, short, long, float, double). When lossy casts are done (e.g. int to byte) the conversion is done modulo the length of the smaller type.
long
8 bytes signed (two's complement). Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.
float
4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative).
Like all numeric types floats may be cast into other numeric types (byte
, short
, long
, int
, double
). When lossy casts to integer types are done (e.g. float
to short
) the fractional part is truncated and the conversion is done modulo the length of the smaller type.
double
char
2 bytes, unsigned, Unicode, 0 to 65,535
Chars are not the same as bytes, ints, shorts or Strings.
資料型別可以分基本資料型別以及使用者自訂型別。
基本資料型別java 內定有8種基本型別。
有整數類 : byte, short , int , long . 資料長度為 8-bits,16-bits,32-bits,64-bits。
浮點數類: double , float. 資料長度為32-bits,64bits
文字類: char資料長度為2bytes且為Unicode`.
邏輯類: boolean .
這8種基本型別有各自的固定記憶體長度跟資料格式。
2010年10月6日 星期三
Hibernate Annotations and Middlegen....etc.
http://fupeg.blogspot.com/2006/07/hibernate-annotations.html
Today I found myself in a familiar position -- needing to write ORM backed code. This was a really simple case actually. I had one new table I needed to add plus a class+DAO to access it from my code. Time to call on my old friend Hibernate.
Perhaps I am "old-fashioned" but I usually like to write DDL first. I can see the logic behind writing an object first and then working from there (object -> mapping -> DDL) but it's just easier for me to write the DDL first. So I wrote that and created my table. Then I fired up Middlegen to create my Hibernate mapping. At that point I paused.
Middlegen is great for generating a Hibernate mapping (hbm) file. I decided not to use this and instead use Java annotations. Hibernate Annotations provides full EJB3 Annotation support for any Hibernate application. So I downloaded it and quickly scanned through some of its documentation. I saw that I needed Hibernate 3.2, so I downloaded the latest version of that 3.2CR3 as 3.2 is not GA yet.
I was already pretty familiar with the EJB3 Annotations as I've been one of the many observers to the evolution of that spec. I was ready to write my annotations, but I still needed a class to annotate. So I went ahead and used Middlegen for that. That required me to generate the hbm file, but I simply discarded it when I was done.
Now I annotated my simple class. I'm using Eclipse 3.2 and it handles annotations very well. I simply write the simple name for the annotation, like @Entity and then I could press ctrl-1 to add the appropriate import statement, just like I would do for a class. It also checked my syntax, so it immediately told me that I didn't need quotes for my nullable attribute when I wrote @Column(name="foo", nullable="false").
Now I had an annotated POJO. Next I wrote the DAO class that accomplished my use cases. They were pretty simple, just two methods. I was using Spring so this was especially easy using its HibernateDaoSupport class.
Next I wrote a simple unit test. I told Eclipse that I wanted a new JUnit test case, and it asked if I wanted to do a 3.x or 4.x test case. I chose 4.x, since I was in an annotative mood. Since this was my first unit test that involved Hibernate Annotations, I decided to keep it as simple as possible and bypass Spring. Thus I simply created my Hibernate session factory in my setUp() method, using the AnnotationConfiguration class, of course.
I ran my test and it failed. Not big surprise, don't all unit tests fail the first time? What was surprising was the error message. I had a stinkin' class not found exception. The class that was not found was org.hibernate.loader.custom.SQLQueryReturn. I looked in my hibernate3.jar and sure enough, it wasn't there!
Luckily it was easy enough to Google for this class and that quickly lead me to a thread on java.net. Turns out while Hibernate Annotations needs Hibernate 3.2, it only works with Hibernate 3.2CR2 not 3.2CR3. In other words, if you download the latest Hibernate and Hibenate Annotations, thye don't work together. I went back to the Hibernate site to download 3.2CR2. I noticed that the general purpose Hibernate download page indicated 3.2CR2 as the latest, even though the home page indicated Hibernate 3.2CR3 was the latest and released on July 6.
So I downloaded 3.2CR2 and used it and ... no problems. I did read on the forums that there definitely were problems with that release, I was just lucky enough not to run into them.
Maybe I should put the annotations on the shelf, for now? I'm guessing that it will be stable well before the code I'm working on will be stable, but it's hard to say given their roadmap...