package ejb1; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; import java.util.Random; import javax.naming.CommunicationException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; public class StatefulEJBClient implements Runnable { private static int liczbaWatkow = 5; private static int minPeriod = 100; // minimalny czas między wywolaniami metody getCounter private static int maxPeriod = 500; // maksymalny czas miedzy wywolaniami metody getCounter public static void main(String[] args) { try { List<Thread> watki = new ArrayList<Thread>(); for (int i = 0; i < liczbaWatkow; i++) { watki.add(new Thread(new StatefulEJBClient(), String.format("Klient %d", i + 1))); } for (Thread t : watki) { t.start(); } } catch (Exception ex) { ex.printStackTrace(); } } public void run() { try { final Context context = getInitialContext(); StatefulEJB statefulEJB = (StatefulEJB) context.lookup("EJB1-statefulStatefulEJB#ejb1.StatefulEJB"); Random generator = new Random(); Thread.sleep(generator.nextInt(maxPeriod - minPeriod + 1) + minPeriod); System.out.println(Thread.currentThread().getName() + ": " + statefulEJB.getHello()); Thread.sleep(generator.nextInt(maxPeriod - minPeriod + 1) + minPeriod); System.out.println(Thread.currentThread().getName() + ": " + statefulEJB.getCounter()); Thread.sleep(generator.nextInt(maxPeriod - minPeriod + 1) + minPeriod); System.out.println(Thread.currentThread().getName() + ": " + statefulEJB.getCounter()); Thread.sleep(generator.nextInt(maxPeriod - minPeriod + 1) + minPeriod); System.out.println(Thread.currentThread().getName() + ": " + statefulEJB.getCounter()); Thread.sleep(generator.nextInt(maxPeriod - minPeriod + 1) + minPeriod); System.out.println(Thread.currentThread().getName() + ": " + statefulEJB.getCounter()); } catch (CommunicationException ex) { System.out.println(ex.getClass().getName()); System.out.println(ex.getRootCause().getLocalizedMessage()); System.out.println("\n*** A CommunicationException was raised. This typically\n*** occurs when the target WebLogic server is not running.\n"); } catch (Exception ex) { ex.printStackTrace(); } } private static Context getInitialContext() throws NamingException { Hashtable env = new Hashtable(); // WebLogic Server 10.x/12.x connection details env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); env.put(Context.PROVIDER_URL, "t3://localhost:7101"); return new InitialContext(env); } }