Enterprise Java On Mac OS X
Enterprise Java On Mac OS X
05/05/10 23:30
Search
Advanced Search Log In | Not a Member? ADC Home > Internet & Web > Java > Support
sh e ll > s ud o m kd i r / us r / l o c a l / jb o s s Pa s sw o r d : sh e ll > s ud o c ho w n l iz : s t a f f / us r / l o c a l /j bo s s
Then move the . zi p file to its installation base directory (if it isnt there already) and unzip it using the j a r command.
sh e ll > m v j b o ss - 3 .0 .2 . z i p / u s r/ l o c a l / j bo ss sh e ll > c d / u s r/ l o ca l/ j b o s s sh e ll > j ar - x f j b os s- 3 . 0 . 2 . z i p
05/05/10 23:30
sh e ll > c hm o d ug + x * .s h
If you happen to be using a version of JBoss prior to 3.0, you may have to fix a glitch in one of the startup scripts. This bug has been fixed in JBoss 3.0. In earlier versions, the ru n. sh script incorrectly identifies a HotSpot server VM (Mac OS X comes standard with only the HotSpot client VM). You can fix this problem by replacing this line in jb o s s/ b i n/ r u n .s h :
The JBoss/Jetty bundle comes pre-configured and ready to run simple J2EE applications. One of the nicer features of JBoss is its ability to instantly deploy an application. Once youve created your application, drop it into the j bo s s/ d e p l oy directory and it will be loaded. Theres no need to re-start the server. In order to compile and deploy the example code in this article, youll need to install the Jakarta projects opensource build tool, called Ant. Im using version 1.4.1 and installed it in / usr /l oc al /an t , with these commands:
In order to use Ant, youll want to have the environment variable $JAVA_HOME set, and you may want to add the Ant bi n directory to your $PATH.
Of course, you can put these commands into a dotfile (a Unix shell startup file) so theyre permanently available. See the man page for your default shell for how to set up dotfiles (for example m an tc sh or m an sh ). Click T er m in a l / P re f e re n c e s /S h e l l from within the Terminal application if you dont know which shell youre running.
05/05/10 23:30
Session beans represent a single clients session. They can be either stateful or stateless. They do not persist beyond a single clients session. Entity beans represent objects maintained in persistent storage (like a database). One of the cool things about using EJB technology is that your application server handles all kinds of tricky things for you. The cost of all these free goodies is that your applications are forced to have a fairly complicated setup process. But after creating a J2EE application or two, youll get the hang of it. And if you shelled out for a graphical J2EE development environment, that tool will automate most or all of the setup process for you. For now, though, Ill show you how to create some simple J2EE applications using only command-line tools. Heres a snapshot of the files necessary to create a Hello World application consisting of a servlet and a session bean.
Here is a tarball that should be helpful for the HelloWorld application, as well as HelloEntity and MailList which we explore below: enterprisejava.tar.gz
The J2EE Platform Specification calls for class and XML files to be bundled together in a specific format for deployment. Web objects, like servlets and their associated we b.x ml files, go into a WAR (Web ARchive) file. EJB classes and their XML config files go into a JAR (Java ARchive) file. Then both those archives get bundled with a file called a pp l i ca t i o n .x m l into a new EAR (Enterprise ARchive) file. Once thats done, you can deploy your new web application by copying the EAR file into your j bos s/ de pl oy directory. First, lets look at the XML files. The first two define a session bean called H el lo Wo rld . The jb oss .x ml file is not strictly required for this application, but Ive included it anyhow. This is the file that would contain vendorspecific configuration information relating to your EJBs. Another file, called j bo ss -w eb. xm l (not shown) can be used for JBoss-specific configuration relating to your web application.
sh e ll > c a t e j b -j a r .x ml <? x ml v e rs i o n =" 1 . 0" e n c o d i n g = "U T F - 8 " ? > <! D OC T Y P E e j b -j a r P UB L I C
http://developer.apple.com/internet/java/enterprisejava.html Pgina 3 de 11
05/05/10 23:30
<! D OC T Y P E e j b -j a r P UB L I C '- / /S u n Mi c r o sy s t em s, I n c . / / D TD E n t e r p ri se Jav aB ea ns 1. 1/ /E N' 'h t tp : / / ja v a . su n . co m/ j 2 e e / d t d s/ e j b - j a r _1 _1 . dtd '> <e j b- j a r > < di s p l ay - n a me > H el lo W o r l d < / di s p l a y - n am e> < en t e r pr i s e -b e a ns > < s e s si o n > < d es c r i pt i o n> He l l o W o r l d E J B < / d e sc ri p tio n> < d is p l a y- n a me >H e l l o W o r l d< / d i s p l a y- na m e> < e jb - n a me > H el lo W o r l d < / e jb - n a m e > < h om e > H el l o Wo rl d H o m e < / h om e > < r em o t e >H e l lo Wo r l d < / r e m ot e > < e jb - c l as s > He ll o W o r l d E J B< / e j b - c l as s> < s es s i o n- t y pe >S t a t e l e s s </ s e s s i o n -t yp e > < t ra n s a ct i o n- ty p e > C o n t a in e r < / t r a ns ac t ion -t yp e> < / s e ss i o n > < /e n t e rp r i s e- b e an s> </ e jb - j a r>
sh e ll > c at j b os s . xm l <? x ml v e rs i o n =" 1 . 0" ?> <j b os s > <e n t e rp r i s e- b e an s> < s es s i o n> < e j b -n a m e> He l l o W o r l d </ e j b - n a m e> < / se s s i on > </ e n t er p r i se - b ea ns > </ j bo s s >
The we b. x m l file should look familiar to you if youve worked with other servlet engines, or have read the Apple Internet Developer articles on Tomcat. This project includes a bare-bones we b. xm l file.
sh e ll > c at w e b. x m l <? x ml v e rs i o n =" 1 . 0" e n c o d i n g = "I S O - 8 8 5 9 -1 "? > <! D OC T Y P E w e b -a p p PU B LI C ' -/ / S u n M i cr os y s t e m s , In c . / / D T D W eb App li ca ti on 2. 2/ /E N' 'h t tp : / / ja v a . su n . co m/ j 2 e e / d t d s/ w e b - a p p _2 .2 . dtd '> <w e b- a p p > <d i s p la y - n am e > He ll o W o r l d < /d i s p l a y - na me > <d e s c ri p t i on > H el lo W o r l d < / de s c r i p t i on > <s e r v le t > <s e r v le t - n am e > He ll o W o r l d S e rv l e t < / s e rv le t -na me > < s er v l e t- c l as s> H e l l o W o r ld S e r v l e t < / se r v l et - c la ss > </ s e r vl e t > <s e r v le t - m ap p i ng > < s er v l e t- n a me >H e l l o W o r l dS e r v l e t < /s er v let -n am e> < u rl - p a tt e r n> /H e l l o W o r l ds < / u r l - p at te r n> </ s e r vl e t - ma p p in g> <e j b - re f > < e jb - r e f- n a me >H e l l o W o r l dH o m e < / e j b- re f -na me > < e jb - r e f- t y pe >S e s s i o n < / ej b - r e f - t yp e> < h om e > H el l o Wo rl d H o m e < / h om e > < r em o t e >H e l lo Wo r l d < / r e m ot e > < e jb - l i nk > H el lo W o r l d < / e jb - l i n k > </ e j b -r e f > </ w eb - a p p>
http://developer.apple.com/internet/java/enterprisejava.html
Pgina 4 de 11
05/05/10 23:30
Finally, heres the a p p l ic at i o n . x m l file, with information about the application as a whole.
sh e ll > c at a p pl i c at io n . x m l <? x ml v e rs i o n =" 1 . 0" e n c o d i n g = "U T F - 8 " ? > <! D OC T Y P E a p p li c a ti on PU B LI C ' -/ / S u n M i cr os y s t e m s , In c . / / D T D J 2E E Ap pl ic at ion 1 .2 // EN' 'h t tp : / / ja v a . su n . co m/ j 2 e e / d t d s/ a p p l i c a ti on _ 1_2 .d td '> <a p pl i c a ti o n > <d i s p la y - n am e > He ll o W o r l d < /d i s p l a y - na me > <d e s c ri p t i on > H el lo W o r l d < / de s c r i p t i on > <m o d u le > < e jb > H e ll o W or ld . j a r < / e j b> < / mo d u l e> <m o d u le > < w eb > < w e b -u r i >H el l o W o r l d . wa r < / w e b - ur i> < c o n te x t -r oo t > H e l l o W or l d < / c o n te xt - roo t> < / we b > </ m o d ul e > </ a pp l i c at i o n >
Next, lets look at the Java code in He l l o W o r l d/ s rc . The first two files, H ell oW or ld .ja va and H el l oW o r l d Ho m e .j a v a , define the home and remote interfaces for the new session bean.
sh e ll > c at H e ll o W or ld . j a v a im p or t j av a . r mi . * ; im p or t j av a x . ej b . *; pu b li c i nt e r f ac e He ll o W o r l d e xt e n d s E J BO bj e ct { p u b l ic S t ri n g h i( ) t h r o w s R e m o t e E x ce pt i on; }
Next is the class file for the bean. This is the file that contains your business logic. It creates a string with the contents hiya. Finally, heres the servlet code. Now that youve got a pile of . x m l files and a pile of .j av a files, you can use Ant to compile your code, bundle it, and deploy it to your application server. By default, when Ant is run, it looks for a file in the current directory called b u i l d. x m l and will follow the directions inside. Heres the bu il d. xml for the Hello World application. With all these files in place, youre ready to run Ant.
sh e ll > a nt
http://developer.apple.com/internet/java/enterprisejava.html
Pgina 5 de 11
05/05/10 23:30
On success, you should see output something like this. Once your application is deployed, you can visit http://localhost:8080/HelloWorld/HelloWorlds in your browser to see all that business logic in action.
Entity Beans
An entity bean represents an object in persistent storage. There are two ways to handle persistence usermanaged and container-managed. In user-managed persistence, the code author must explicitly store and retrieve the beans data from storage, usually via JDBC calls. In container-managed persistence, the EJB container handles all the storage and retrieval behind the scenes. JBoss comes pre-configured to use the HypersonicSQL embedded database, which means you can run this example code without changing your JBoss configuration. If youd like to use a different database, such as MySQL, see the JBoss documentation. Heres another simple web application. This one uses an entity bean to create, store, and retrieve user records. A user record consists of two data fields: name, and email. Notice that even though the records are being stored in the HypersonicSQL database, I didnt write a line of database code. Instead, I specify the fields in my e j b j ar . xm l file, and rely on my EJB container to manage persistence. Here is a listing of the other XML files this application uses. Now to the Java source. First, the remote and home interfaces, the entity bean, the servlet, and the bu i l d. x m l file. You might have noticed by now that the build files look a lot alike. In fact, the only thing I really had to change was the appname property (although I also changed the project name). The rest of the file is pretty standard, and I hope you can re-use it for your projects. You will have to customize your bu il d.x ml files further, of course, once youre building more complicated applications, using packages, and requiring more libraries in your compilation path. My advice is that you either get to know Ant, or invest in a Java IDE with project management capabilities. Once deployed (again, just type a n t once your files and directories are in place), your application should be available via http://localhost:8080/HelloEntity/Hi.
http://developer.apple.com/internet/java/enterprisejava.html
Pgina 6 de 11
05/05/10 23:30
http://developer.apple.com/internet/java/enterprisejava.html
Pgina 7 de 11
05/05/10 23:30
Transactions
Transactions are multi-part actions that are required to function as one unit. Either all actions succeed together, or they all fail. Just as an entity beans data persistence may be user- or container-managed, a J2EE transaction can be handled by your bean code, or handed off to the container. If the container is managing a transaction associated with a certain method, it will automatically roll back any database calls in that method upon receiving a system exception. For more on transactions, visit this tutorial on java.sun.com. You can define container-managed transactions in your beans deployment descriptor (the e jb -ja r. xm l file). See the assembly-descriptor section of http://java.sun.com/dtd/ejb-jar_2_0.dtd for full details. In general, your code
http://developer.apple.com/internet/java/enterprisejava.html Pgina 8 de 11
05/05/10 23:30
JBoss comes with a built-in transaction manager. Like other JBoss component parts, it can be replaced with another JTA (Java Transaction API) transaction manager implementation. See the JBoss documentation for instructions.
http://developer.apple.com/internet/java/enterprisejava.html
Pgina 9 de 11
05/05/10 23:30
http://developer.apple.com/internet/java/enterprisejava.html
Pgina 10 de 11
05/05/10 23:30
Get information on Apple products. Visit the Apple Store online or at retail locations. 1-800-MY-APPLE Copyright 2010 Apple Inc. All rights reserved. | Terms of use | Privacy Notice
http://developer.apple.com/internet/java/enterprisejava.html
Pgina 11 de 11