EJB Tutorial from JBoss: clustering
File: Session.java /* * JBoss, Home of Professional Open Source. * Copyright 2006, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.tutorial.clustering.bean; /** * Comment * * @author <a href="mailto:bill@jboss.org">Bill Burke</a> * @version $Revision: 57207 $ */ public interface Session { void test(); } File: SessionBean.java /* * JBoss, Home of Professional Open Source. * Copyright 2006, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.tutorial.clustering.bean; import javax.ejb.Stateless; import javax.ejb.Remote; import org.jboss.annotation.ejb.Clustered; import org.jboss.annotation.ejb.Clustered; /** * Comment * * @author <a href="mailto:bill@jboss.org">Bill Burke</a> * @version $Revision: 57207 $ */ @Stateless @Clustered @Remote(Session.class) public class SessionBean implements Session { public void test() { System.out.println("hello!!!"); } } File: StatefulBean.java /* * JBoss, Home of Professional Open Source. * Copyright 2006, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.tutorial.clustering.bean; import javax.ejb.Stateful; import javax.ejb.Remote; import org.jboss.annotation.ejb.Clustered; /** * Comment * * @author <a href="mailto:bill@jboss.org">Bill Burke</a> * @version $Revision: 57207 $ */ @Stateful @Clustered @Remote(StatefulRemote.class) public class StatefulBean implements java.io.Serializable, StatefulRemote { private int state = 0; public void increment() { System.out.println("counter: " + (++state)); } public String toString() { return super.toString() + " state-(" + state + ")"; } } File: StatefulRemote.java /* * JBoss, Home of Professional Open Source. * Copyright 2006, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.tutorial.clustering.bean; /** * Comment * * @author <a href="mailto:bill@jboss.org">Bill Burke</a> * @version $Revision: 57207 $ */ public interface StatefulRemote { void increment(); } File: StatefulRun.java /* * JBoss, Home of Professional Open Source. * Copyright 2006, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.tutorial.clustering.client; import org.jboss.tutorial.clustering.bean.StatefulRemote; import javax.naming.InitialContext; public class StatefulRun { public static void main(String[] args) throws Exception { InitialContext ctx = new InitialContext(); StatefulRemote remote = (StatefulRemote) ctx.lookup("StatefulBean/remote"); while (true) { remote.increment(); Thread.sleep(10000); } } } File: StatelessRun.java /* * JBoss, Home of Professional Open Source. * Copyright 2006, Red Hat Middleware LLC, and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.tutorial.clustering.client; import org.jboss.tutorial.clustering.bean.Session; import javax.naming.InitialContext; public class StatelessRun { public static void main(String[] args) throws Exception { InitialContext ctx = new InitialContext(); Session remote = (Session) ctx.lookup("SessionBean/remote"); while (true) { remote.test(); Thread.sleep(1000); } } }
1. | EJB Tutorial from JBoss: Clustered Entity | ![]() |