Blog platform in EJB 3.1
Assignment!
We need to create EJB layer for a blog site.
Clients communicate with stateful session bean. It is necessary to implement the method for search blogs and method for setting up a new blog.
In the case of passivation of the stateful session bean , we need just before passivation to print a message on the console about session bean is going to be pasive.
Singleton bean keeps customer information (name , username , password ... ) and data on blogs ( text, date , number of like- s) in ArrayList.
Singleton is NOT instantiated immediately upon deployment, but by the first call. By instantiate initialize ArrayList in which data need to be stored.
If we call the method that is used to search, interceptor intercepts that method and he update statistics (number of views for each blog).
Call method to set up a new blog interceptor intercepts that method and he update statistics on the number of blogs set up during the day
The statistic data is stored in the same Singleton which are stored and other information .
All interceptor's is required to be implemented in a separate class.
It is necessary to implement a timer that starting from 1.1.2014 . Every day at 00:00 timer need to print on console how many visit blog's have that day.
Please share this post if you like it.
Singleton Class
import java.util.ArrayList;
import java.util.Date;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
/**
* Developed by codeMaker
*/
@Singleton
@LocalBean
public class Baza implements BazaLocal {
public ArrayList<Klijent> listaKlijenata = new ArrayList<Klijent>();
public ArrayList<Blog> listaBlogova = new ArrayList<Blog>();
int brojPB = 0;
public int getBrojPB() {
return brojPB;
}
public void setBrojPB(int brojPB) {
this.brojPB = brojPB;
}
public void setListaBlogova(ArrayList<Blog> listaBlogova) {
this.listaBlogova = listaBlogova;
}
public void setListaKlijenata(ArrayList<Klijent> listaKlijenata) {
this.listaKlijenata = listaKlijenata;
}
public ArrayList<Klijent> getListaKlijenata() {
return listaKlijenata;
}
public ArrayList<Blog> getListaBlogova() {
return listaBlogova;
}
public Baza() {
// TODO Auto-generated constructor stub
Klijent klijent = new Klijent("Niki", "Re", "user", "pass");
listaKlijenata.add(klijent);
Blog blog = new Blog("tekst", new Date());
listaBlogova.add(blog);
}
}
SingletonLocal interface
import java.util.ArrayList;
import javax.ejb.Local;
@Local
public interface BazaLocal {
ArrayList<Klijent> getListaKlijenata();
ArrayList<Blog> getListaBlogova();
void setListaBlogova(ArrayList<Blog> lista);
void setListaKlijenata(ArrayList<Klijent> lista);
int getBrojPB();
void setBrojPB(int brojPB);
}
Klijent class
import java.util.ArrayList;
public class Klijent {
public String ime;
public String prezime;
public String username;
public String pass;
public ArrayList<Blog> listaBlogova;
public Klijent(String ime, String prezime, String username, String pass) {
this.ime = ime;
this.prezime = prezime;
this.username = username;
this.pass = pass;
listaBlogova = new ArrayList<Blog>();
}
public String getIme() {
return ime;
}
public void setIme(String ime) {
this.ime = ime;
}
public String getPrezime() {
return prezime;
}
public void setPrezime(String prezime) {
this.prezime = prezime;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public ArrayList<Blog> getListaBlogova() {
return listaBlogova;
}
public void setListaBlogova(ArrayList<Blog> listaBlogova) {
this.listaBlogova = listaBlogova;
}
}
Blog class
import java.util.Date;
public class Blog {
public String tekst;
public Date datum;
public int brojLajkova;
public int brojPregleda;
public Blog(String tekst, Date datum) {
this.tekst = tekst;
this.datum = datum;
brojLajkova = 0;
brojPregleda = 0;
}
public String getTekst() {
return tekst;
}
public void setTekst(String tekst) {
this.tekst = tekst;
}
public Date getDatum() {
return datum;
}
public void setDatum(Date datum) {
this.datum = datum;
}
public int getBrojLajkova() {
return brojLajkova;
}
public void setBrojLajkova(int brojLajkova) {
this.brojLajkova = brojLajkova;
}
public int getBrojPregleda() {
return brojPregleda;
}
public void setBrojPregleda(int brojPregleda) {
this.brojPregleda = brojPregleda;
}
Stateful bean Blogovanje - Clients communicate with him
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.PrePassivate;
import javax.ejb.Stateful;
import javax.interceptor.Interceptors;
@Stateful
@LocalBean
public class Blogovanje implements BlogovanjeRemote {
@EJB BazaLocal baza;
public Blogovanje() {
}
@Interceptors(PretragaInterceptor.class)
public String pretraga(String upit) {
List<Blog> lista = baza.getListaBlogova();
for (Blog b : lista) {
String tekst = b.getTekst();
if (tekst.equalsIgnoreCase(upit)) {
return "Blog je pronadjen!";
}
}
return "Blog nije pronadjen.";
}
@Interceptors(PretragaInterceptor.class)
public String postaviBlog(String username, String pass, String tekstBloga) {
int uspjeh = 0;
ArrayList<Klijent> lista = baza.getListaKlijenata();
for(Klijent k : lista) {
String user = k.getUsername();
String p = k.getPass();
if((username.equalsIgnoreCase(user)) && (pass.equalsIgnoreCase(p))) {
Blog b = new Blog(tekstBloga, new Date());
ArrayList<Blog> listaBlogova = baza.getListaBlogova();
listaBlogova.add(b);
baza.setListaBlogova(listaBlogova);
k.getListaBlogova().add(b);
uspjeh++;
}
}
baza.setListaKlijenata(lista);
if (uspjeh > 0) {
return "Uspeh";
} else {
return "Nismo uspjeli da postavimo blog, user i pass ne odgovaraju!";
}
}
@PrePassivate
public void prijePasiviziranja() {
System.out.println("Jagoda u supermarketu.");
}
}
BlogovanjeRemote interface - For session bean
Methods in this class will be visible for clients
import javax.ejb.Remote;
@Remote
public interface BlogovanjeRemote {
String pretraga(String upit);
String postaviBlog(String username, String pass, String tekstBloga);
}
Interceptors in one class you can build it in seperate classes.
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class PretragaInterceptor {
@EJB BazaLocal baza;
@AroundInvoke
public Object brojPregledaStatistika(InvocationContext ic) throws Exception {
String imeMetoda = ic.getMethod().getName();
if (imeMetoda.equalsIgnoreCase("pretraga")) {
Object [] listaO = ic.getParameters();
String upit = (String) listaO[0];
List<Blog> lista = baza.getListaBlogova();
for (Blog b : lista) {
String tekst = b.getTekst();
if (tekst.equalsIgnoreCase(upit)) {
b.setBrojPregleda(b.getBrojPregleda() + 1);
}
}
return ic.proceed();
}
if (imeMetoda.equalsIgnoreCase("postaviBlog")) {
Object [] listaO = ic.getParameters();
String username = (String) listaO[0];
String pass = (String) listaO[1];
ArrayList<Klijent> lista = baza.getListaKlijenata();
for(Klijent k : lista) {
String user = k.getUsername();
String p = k.getPass();
if((username.equalsIgnoreCase(user)) && (pass.equalsIgnoreCase(p))) {
baza.setBrojPB(baza.getBrojPB() + 1);
}
}
return ic.proceed();
}
return ic.proceed();
}
}
Timer class
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.TimerService;
@Startup
@Singleton
@LocalBean
public class IspisTimer {
@EJB BazaLocal baza;
@Resource TimerService ts;
@Timeout
public void ispsi() {
System.out.println("Danas je postavljeno: " + baza.getBrojPB() + " blogova!");
baza.setBrojPB(0);
}
@PostConstruct
public void startuj(){
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
Date date = null;
try {
date = sdf.parse("31-12-2013 23:59");
} catch (Exception e) { }
ts.createTimer(date, 86400000, null);
}
}
And Klijent app, so we can test it.
import java.util.Hashtable;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import binovi.Blogovanje;
import binovi.BlogovanjeRemote;
public class Klijent {
static Context initialContext;
public static void main(String[] args) {
try {
Context context = getInitialContext();
String name = getLookUpName();
BlogovanjeRemote bean = (BlogovanjeRemote) context.lookup(name);
System.out.println();
System.out.println(bean.postaviBlog("user", "pass", "text"));
System.out.println(bean.postaviBlog("ssa2", "agawa", "text"));
System.out.println(bean.pretraga("text"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getLookUpName() {
String appName = "";
String moduleName = "Blogovanje";
String distinctName = "";
String beanName = Blogovanje.class.getSimpleName();
String interfaceName = BlogovanjeRemote.class.getName();
String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName
+ "/" + beanName + "!" + interfaceName+"?stateful";
System.out.println(name);
return name;
}
public static Context getInitialContext() {
if (initialContext == null) {
try {
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES,
"org.jboss.ejb.client.naming");
initialContext = new InitialContext(jndiProperties);
} catch (Exception e) {
e.printStackTrace();
}
}
return initialContext;
}
}
If you have any questions or need help with coding post a comment bellow.
Please share this post if you like it.
Great example, thx.
ReplyDelete