Skip to content

Instantly share code, notes, and snippets.

View abhirockzz's full-sized avatar
👋
fmt.Println(hello, world!)

Abhishek Gupta abhirockzz

👋
fmt.Println(hello, world!)
View GitHub Profile
@Path("/feed")
public class RESTFeed {
@Inject
Event<String> event;
@POST
@Consumes(MediaType.TEXT_PLAIN)
public void push(String msg) {
event.fire(msg);
public void onMsg(@Observes String msg) {
//different WS enpoint instance - notice the hash code value in the server log
System.out.println("WS End point class ID -- " + this.hashCode());
try {
client.getBasicRemote().sendText(msg);
} catch (IOException ex) {
Logger.getLogger(ServerEndpoint.class.getName()).log(Level.SEVERE, null, ex);
private static Set<Session> peers = Collections.synchronizedSet(new HashSet());
@abhirockzz
abhirockzz / EJB1.java
Created February 19, 2015 10:05
Valid CDI scopes for Stateless beans
//This is valid
@Stateless
@Dependent
public class EJB1 {
@PostConstruct
public void init(){
System.out.println(EJB1.class.getName() + " constructed successfully on "+ new Date().toString());
}
}
@abhirockzz
abhirockzz / Singleton1.java
Created February 19, 2015 10:08
Valid CDI scopes for Singleton EJBs
//This is legal
@Singleton
@Startup
@ApplicationScoped
public class Singleton1 {
@PostConstruct
public void init(){
System.out.println(Singleton1.class.getName() + " constructed successfully on "+ new Date().toString());
}
@abhirockzz
abhirockzz / RESTfulResource.java
Created February 20, 2015 09:20
Create a CacheControl instance manually
@Path("/testcache")
public class RESTfulResource {
@GET
@Produces("text/plain")
public Response find(){
CacheControl cc = new CacheControl();
cc.setMaxAge(20);
return Response.ok(UUID.randomUUID().toString()).cacheControl(cc).build();
}
}
@abhirockzz
abhirockzz / CachControlConfig.java
Last active August 29, 2015 14:15
A custom annotation to configure Cache Control parameters
@Retention(RUNTIME)
@Target({FIELD, PARAMETER})
public @interface CachControlConfig {
public boolean isPrivate() default true;
public boolean noCache() default false;
public boolean noStore() default false;
public boolean noTransform() default true;
public boolean mustRevalidate() default true;
public boolean proxyRevalidate() default false;
@abhirockzz
abhirockzz / CacheControlFactory.java
Created February 20, 2015 09:23
A CDI Producer (factory)
public class CacheControlFactory {
@Produces
public CacheControl get(InjectionPoint ip) {
CachControlConfig ccConfig = ip.getAnnotated().getAnnotation(CachControlConfig.class);
CacheControl cc = null;
if (ccConfig != null) {
cc = new CacheControl();
cc.setMaxAge(ccConfig.maxAge());
@Path("/testcache")
public class RESTfulResource {
@Inject
@CachControlConfig(maxAge = 20)
CacheControl cc;
@GET
@Produces("text/plain")
public Response find() {
return Response.ok(UUID.randomUUID().toString()).cacheControl(cc).build();
@abhirockzz
abhirockzz / Books.java
Last active August 29, 2015 14:16
A hypothetical resource of books
@Path("books")
public class Books{
@Produces("application/json")
@GET
public List<Book> findAll(){
//find all books
}
@Produces("application/json")
@GET
@Path("{id}")