001/* 002 GRANITE DATA SERVICES 003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S. 004 005 This file is part of Granite Data Services. 006 007 Granite Data Services is free software; you can redistribute it and/or modify 008 it under the terms of the GNU Library General Public License as published by 009 the Free Software Foundation; either version 2 of the License, or (at your 010 option) any later version. 011 012 Granite Data Services is distributed in the hope that it will be useful, but 013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License 015 for more details. 016 017 You should have received a copy of the GNU Library General Public License 018 along with this library; if not, see <http://www.gnu.org/licenses/>. 019*/ 020 021package org.granite.tide.seam.async; 022 023import static org.jboss.seam.annotations.Install.FRAMEWORK; 024 025import org.granite.gravity.Gravity; 026import org.granite.gravity.GravityManager; 027import org.granite.tide.async.AsyncPublisher; 028import org.jboss.seam.ScopeType; 029import org.jboss.seam.annotations.AutoCreate; 030import org.jboss.seam.annotations.Install; 031import org.jboss.seam.annotations.Name; 032import org.jboss.seam.annotations.Scope; 033import org.jboss.seam.annotations.intercept.BypassInterceptors; 034import org.jboss.seam.contexts.ServletLifecycle; 035 036import flex.messaging.messages.AsyncMessage; 037 038 039/** 040 * Async publisher using Gravity to send messages to the client 041 * 042 * @author William DRAI 043 */ 044@Name("org.granite.tide.seam.async.publisher") 045@Install(precedence=FRAMEWORK, classDependencies={"org.granite.gravity.Gravity"}) 046@Scope(ScopeType.STATELESS) 047@BypassInterceptors 048@AutoCreate 049public class SeamAsyncPublisher implements AsyncPublisher { 050 051 public static final String DESTINATION_NAME = "seamAsync"; 052 053 private Gravity getGravity() { 054 return GravityManager.getGravity(ServletLifecycle.getServletContext()); 055 } 056 057 public void initThread() { 058 Gravity gravity = getGravity(); 059 if (gravity == null) 060 throw new RuntimeException("Gravity service not configured, it is required for asynchronous event publishing"); 061 062 gravity.initThread(null, null); 063 } 064 065 public void publishMessage(String sessionId, Object body) { 066 AsyncMessage message = new AsyncMessage(); 067 message.setHeader(AsyncMessage.SUBTOPIC_HEADER, "tide.events." + sessionId); 068 message.setDestination(DESTINATION_NAME); 069 message.setBody(body); 070 071 getGravity().publishMessage(message); 072 } 073}