RandomQuoteBean.java
View Code
1 /* 2 * To change this template, choose Tools | Templates 3 * and open the template in the editor. 4 */ 5 package beans; 6 7 import java.util.Random; 8 import javax.ejb.Stateless; 9 10 /** 11 * 12 * @author teemu 13 */ 14 @Stateless 15 public class RandomQuoteBean implements RandomQuoteBeanRemote { 16 17 private String[] quotes = new String[]{ 18 "640K ought to be enough for anybody. --Bill Gates in 1981", 19 "The Internet is so big, so powerful and pointless that for some people it is a complete substitute for life. --Andrew Brown", 20 "I think there is a world market for maybe five computers. --Thomas Watson in 1943", 21 "Computers are useless. They can only give you answers. -- Pablo Picasso", 22 "Never trust a computer you can’t throw out a window. -- Steve Wozniak", 23 "There are two major products that come out of Berkeley: LSD and UNIX. We don’t believe this to be a coincidence. --Jeremy S. Anderson", 24 "The bulk of all patents are crap. Spending time reading them is stupid. It’s up to the patent owner to do so, and to enforce them. -- Linux Torvalds", 25 "There’s an old story about the person who wished his computer were as easy to use as his telephone. That wish has come true, since I no longer know how to use my telephone. --Bjarne Stroustrup" 26 }; 27 28 @Override 29 public String getRandomQuote() { 30 Random r = new Random(System.currentTimeMillis()); 31 return (quotes[r.nextInt(quotes.length-1)]); 32 } 33 }
RandomQuoteBeanRemote.java
View Code
1 /* 2 * To change this template, choose Tools | Templates 3 * and open the template in the editor. 4 */ 5 package beans; 6 7 import javax.ejb.Remote; 8 9 /** 10 * 11 * @author teemu 12 */ 13 @Remote 14 public interface RandomQuoteBeanRemote { 15 16 String getRandomQuote(); 17 18 }
RandomQuoteClient.java
View Code
1 /* 2 * To change this template, choose Tools | Templates 3 * and open the template in the editor. 4 */ 5 package client; 6 7 import beans.RandomQuoteBeanRemote; 8 import java.util.Scanner; 9 import java.util.logging.Level; 10 import java.util.logging.Logger; 11 import javax.naming.Context; 12 import javax.naming.InitialContext; 13 import javax.naming.NamingException; 14 import javax.naming.NoInitialContextException; 15 16 /** 17 * 18 * @author teemu 19 */ 20 public class RandomQuoteClient { 21 22 /** 23 * @param args the command line arguments 24 */ 25 public static void main(String[] args) { 26 RandomQuoteBeanRemote bean; 27 Context context; 28 try { 29 30 // locate the bean by using JNDI 31 context = new InitialContext(); 32 Object obj = context.lookup("java:global/RandomQuoteApplication-ejb/RandomQuoteBean"); 33 bean = (RandomQuoteBeanRemote) obj; 34 35 System.out.println("Input a number indicating how many random quotes will be fetched from the bean (server)."); 36 Scanner reader = new Scanner(System.in); 37 38 // request and print given number of quotes from the bean 39 int i = reader.nextInt(); 40 for( int j=0; j<i; j++) 41 System.out.println(bean.getRandomQuote()); 42 43 } catch (NoInitialContextException nicex) { 44 nicex.printStackTrace(); 45 } catch (NamingException nex) { 46 nex.printStackTrace(); 47 } 48 49 } 50 }