Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Enterprise-Scale Software Development (COMP5348) 
Tutorial Week 7, Semester 1, 2010 
Part A – Configuring a JMS Queue 
1. Download the source package, unzip it. You should see a “messagebox-jms” folder and two files 
called ‘start_server.bat ‘ and ‘stop_server.bat’. 
2. Double click on ‘start_server.bat’ to start your application server. May only work in SIT labs. 
Remember to use ‘stop_server.bat’ when you are done. 
3. Go to the Admin console http://localhost:4848  
user: admin password: adminadmin 
4. Under Resources  JMS Resources  Connection Factories.  
5. Create a new ConnectionFactory with JNDI Name “jms/MessageBoxConnectionFactory” and 
Type javax.jms.ConnectionFactory. 
6. Under Resources  JMS Resources  Destination Resources. 
7. Create a new Destination with JNDI Name “jms/MessageBoxQueue” and type javax.jms.Queue 
and Physical Destination Name property of “MessageBoxQueue”. 
8. This step is IMPORTANT if you are working in the SIT Lab: 
In the Admin Console, go to Configuration  Java Message Service  JMS Hosts  
default_JMS_host. Set the Host to “localhost”. Save. 
If you are working in the Undergraduate Access Lab, your computer will appear to hang/slow 
down when running the following programs especially through Netbeans. This is unavoidable 
but the programs will work.  
Part B – Sending and Receiving Messages 
 
1. Import the messagebox-jms project into NetBeans 6.8. 
2. You need to resolve the Missing Server problem by adding a new server. Right-click on 
‘messagebox-jms’ (should be in red) and click ‘Resolve Missing Server..’. Follow the screens and 
use the following parameters: 
Server:  GlassFish v2.x 
Server Location: C:\Sun\AppServer 
Admin Username and Password: admin, adminadmin 
 
3. In NetBeans, run messagebox-jms.MsgBoxProducer. Note the output. 
4. Now run MsgBoxAsync. Note output and comment. Input ‘q’ to end program. 
5. Now run MsgBoxSync.  
6. Run MsgBoxProducer again. Note output of MsgBoxSync and comment. 
7. Review the code for MsgBoxProducer, MsgBoxAsync and MsgBoxSync. 
Stop the server 
Run ‘stop_server.bat’ when you are done. 
Code Overview 
MsgBoxProducer connects to the JMS queue that was setup in Part A and sends to it a text message 
every second for 100 messages followed by a terminator message. 
MsgBoxAsync connects to the same queue and reads the messages asynchronously. It registered a 
listener with the queue that is called whenever a message is available. The main thread does not 
have to keep checking for available messages and hence is free to continue its execution. 
MsgBoxSync is an example of synchronous reading from the queue. The main thread runs in a loop 
that keeps asking the queue for available messages every 2 seconds (aka polling). Here, the loop 
ends when the terminator message is read. 
Notice that in asynchronous mode, a message would be read close to every 1 second. In comparison, 
in synchronous mode, the queue would be polled every 2 seconds and 2 messages would be read 
within that poll although messages were sent every 1 second. 
So in asynchronous mode, the reader gets the message as soon as the message is ready for reading. 
This can be near the frequency of the source (but expect some random delay). Synchronous mode 
allows some control on the frequency the data is read.