10

I am going to implement something similar to Facebook notification and this website (StackOverflow's notification which is notifying us if anyone write a comment/answer etc for our question). Please note users are going to use my application as a website not mobile application.

I came across following answer which fetch the results, but I need to push the results not fetch.

Based on suggestions I have created a simple method in my entity class and added the @PostPersist to it but it has not worked so based on this answer I added the persistence.xml file to define the listeners but after session.save(user) the aftersave method does not get triggered.

User.java

@Entity
public class User{
  .....
  @PostPersist
    public void aftersave(){
        System.err.println("*****this is post persist method****");
    }
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->

<property name="hibernate.ejb.event.pre-insert"  value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.pre-update"  value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.pre-delete"  value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-insert" value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-update" value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-delete" value="my.hibernate.events.listeners.Listener" />

pom.xml

 <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.1.Final</version>
            <type>jar</type>
        </dependency>
Community
  • 1
  • 1
J888
  • 1,944
  • 8
  • 42
  • 76
  • 1
    Depends on the database engine you are using; most don't support change listeners, here is an example from the [Postgres docs](http://jdbc.postgresql.org/documentation/81/listennotify.html). You could probably use a [hibernate listener](http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/listeners.html), the `@PostPersist` on the entity you want ought to do the trick. – Boris the Spider Jun 27 '13 at 07:14
  • @BoristheSpider question is updated – J888 Jun 28 '13 at 06:26

6 Answers6

13

Sounds like a task for WebSockets. It is part of Java EE 7 so the Glassfish should be one of the first AS that will support it.

For intercepting the DB access you can use @PostUpdate and @PostPersist. Here is related question.

There are many ways to do the so called Server Push for notifying the connected clients:

EDIT: In the Java world, there are couple of frameworks where server push (reverse ajax) is implemented out-of-the box. If you are familiar with GWT, I would suggest Errai. Other alternative is the Atmospere. The downside of the Atmospere is the fact that it requires standalone running process next to your regular application server with your web app. I was playing with it a year ago so this may have been changed since then.

In general, it is hard to provide you with a concrete piece of code, because it depends on the framework you will choose. I am familiar with Errai so here is an example in it:

Server Side:

@ApplicationScoped
public class TickerService {

  @Inject
  private Event<Tick> tickEvent;

  private void sendTick() {
    tickEvent.fire(new Tick());
  }
} 

Client Side:

@EntryPoint
public class TickerClient {
  public void tickHappened(@Observes Tick tick) {

    // update the UI with the new data
  }
}

Other benefits of using the Errai is having the CDI on the server and on the client out-of-the-box, another thing that is nice is using the web-sockets under the covers if it is supported and falling back to other solutions otherwise.

Whatever you choose, it should fit to your existing infrastructure and to your client side UI framework.

Community
  • 1
  • 1
Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
  • 2
    The trick isn't so much the db... (since jpa/hibernate provide clear solutions there) but how to implement the ajax. There is a framework for this purpose called atmosphere, might be worth checking out: http://stackoverflow.com/questions/4003102/best-java-framework-for-server-side-websockets it tries to account for client limitations although I hope in the not distant future websockets will be the only sensible option for this route. – Quaternion Jun 27 '13 at 19:43
4

mqtt can be used for server pushing and message broadcasting.

There are more detail information in http://mqtt.org/.

======================================

Updated: Jul 11, 2013

Mqtt is a publish/subscribe, extremely simple and lightweight messaging protocol. If server is a publisher and client browser subscribe the topic which server publish to, then server can push message to client directly.

Some useful resource:

Mosquitto is an open sourced mqtt server. Easy to install and configure.

mqtt-client is a proven powerful java mqtt client.

oldmonk
  • 739
  • 4
  • 10
4

Use Node JS and socket.io

This technology chooses the best transportation method based on the browser that the client is using.

For latest browsers it uses Web Sockets and for others it degrades gracefully to Flash Socket or Long Pooling. See more here

What you need to do is set up a server using these technologies. The server would run at a particular port. All clients would listen to that port and server would be able to push data to the client through that port.

Manu
  • 901
  • 1
  • 8
  • 28
  • it is a website does it work on website as well? or just on client server platform? – J888 Jul 19 '13 at 07:03
  • This technology is meant for browsers only. You create a client-server platform yourself. All browsers would act as clients and you will write some code to build a server. See socket.io website for sample code – Manu Jul 19 '13 at 07:16
4

Comet also known as Reverse Ajax, is a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it.

Comet (AKA long lived http, server push) allows the server to start answering the browser's request for information very slowly, and to continue answering on a schedule dictated by the server. For more information about Comet, see the following:

DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible. With Reverse Ajax, DWR allows Java code running on a server to use client side APIs to publish updates to arbitrary groups of browsers. This allows interaction 2 ways - browser calling server and server calling browser. DWR supports Comet, Polling and Piggyback (sending data in with normal requests) as ways to publish to browsers.

DWR provides integration with Spring, Struts, Guice, Hibernate and others. You can read more from here.

Other Comet and Reverse AJAX frameworks:

Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
2

but after session.save(user) the aftersave method does not get triggered.

  • @PostPersist is a JPA callback.
  • session.save() is a non-JPA, hibernate proprietary method. JPA uses entityManager.persist().
  • you're using incompatible features
Glen Best
  • 22,769
  • 3
  • 58
  • 74
  • So whats the solution for hibernate to do the same? – J888 Jul 04 '13 at 23:08
  • Firstly, I always go for the standard, where available. In this case, JPA - Use EntityManager & @PostPersist and refer to http://docs.jboss.org/hibernate/devguide/en-US/html/. Secondly I looked at Hibernate proprietary doc and could find no mention of "callback" or "post", but seems you can override Envers listeners to inform you of events. Not entirely clean, but should work, *if that's what you want*: http://docs.jboss.org/hibernate/devguide/en-US/html/ch15.html – Glen Best Jul 05 '13 at 04:25
  • is it efficient to combine JPA and hibernate ? – J888 Jul 05 '13 at 05:11
  • Not exactly inefficient. More complex. I certaintly wouldn't mix the two modes unless you have a compelling reason. Some people use JPA for everything possible & then fallback to "raw hibernate" when they need an extra feature. But this is: (a) complex (b) quite rare, because JPA is very rich. Of course there are many "legacy" users out there using just raw hibernate who haven't yet switched to JPA - but then, they miss the new features added by JPA. :) – Glen Best Jul 05 '13 at 05:26
2

Check for update from server on every 30 Seconds or as per requirement.

window.setInterval(function(){
  /// call your function here
 //Make AJAX call
 //Update Respective HTML Contact i,e, DIV

}, 30000);
Vinit Prajapati
  • 1,593
  • 1
  • 17
  • 29