实现基于事件机制的portlet间通信

portlet间通信包括 : 本地portlet与本地portlet通信
                     远程portlet与远程portlet通信
                     本地portlet与远程portlet通信

JSR286规范中定义了本地portlet之间的通信,共享session ,共享资源、事件机制

通过event进行portlet之间通信:

1. 定义publishing event的portlet,定义event-definition ,及supported-publishing-event

<?xml version="1.0"?>

<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">
	
	<portlet>
		<portlet-name>foodaoportlet</portlet-name>
		<display-name>FooDaoPortlet</display-name>
		<portlet-class>com.liferay.wyy.dao.DAOPortlet</portlet-class>
		<init-param>
			<name>view-template</name>
			<value>/view/view.jsp</value>
		</init-param>
		<expiration-cache>0</expiration-cache>
		<supports>
			<mime-type>text/html</mime-type>
			<portlet-mode>view</portlet-mode>
		</supports>
		<portlet-info>
			<title>FooDaoPortlet</title>
			<short-title>FooDaoPortlet</short-title>
			<keywords></keywords>
		</portlet-info>
		<security-role-ref>
			<role-name>administrator</role-name>
		</security-role-ref>
		<security-role-ref>
			<role-name>guest</role-name>
		</security-role-ref>
		<security-role-ref>
			<role-name>power-user</role-name>
		</security-role-ref>
		<security-role-ref>
			<role-name>user</role-name>
		</security-role-ref>
		<supported-processing-event>
			<qname xmlns:x="http://wangyy.example.com/food">x:foodItem</qname>
		</supported-processing-event>
		<!-- 定义支持发布的event -->
		<supported-publishing-event>
			<qname xmlns:x="http://wangyy.example.com/food">x:foodItem</qname>
		</supported-publishing-event>
	</portlet>
	
	<!-- 支持event定义 -->
	<event-definition xmlns:x="http://wangyy.example.com/food">
		<qname>x:foodItem</qname>
		<value-type>com.liferay.wyy.dao.model.FoodItem</value-type>
	</event-definition>
</portlet-app>


2.定义处理event的portlet, 定义supported-processing-event 及event-definition

<?xml version="1.0"?>

<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">
	
	<portlet>
		<portlet-name>eventprocessportlet</portlet-name>
		<display-name>EventProcessPortlet</display-name>
		<portlet-class>
			com.liferay.wyy.event.portlet.EventProcessPortlet
		</portlet-class>
		<init-param>
			<name>view-template</name>
			<value>/view/view.jsp</value>
		</init-param>
		<expiration-cache>0</expiration-cache>
		<supports>
			<mime-type>text/html</mime-type>
			<portlet-mode>view</portlet-mode>
		</supports>
		<portlet-info>
			<title>EventProcessPortlet</title>
			<short-title>EventProcessPortlet</short-title>
			<keywords></keywords>
		</portlet-info>
		<security-role-ref>
			<role-name>administrator</role-name>
		</security-role-ref>
		<security-role-ref>
			<role-name>guest</role-name>
		</security-role-ref>
		<security-role-ref>
			<role-name>power-user</role-name>
		</security-role-ref>
		<security-role-ref>
			<role-name>user</role-name>
		</security-role-ref>
		<!-- 定义支持处理的event 需要放在publishing-event之前-->
		<supported-processing-event>
			<qname xmlns:x="http://wangyy.example.com/food">x:foodItem</qname>
		</supported-processing-event>
	</portlet>
</portlet-app>


3. 发布event的portlet中处理:
在processAction中添加setEvent代码
//setEvent
                QName qName = new QName("http://wangyy.example.com/food", "foodItem");
                res.setEvent(qName, foodItem);


4.处理event的portlet中代码
/**
     * 处理event事件
     * @param request
     * @param response
     * @throws PortletException
     * @throws IOException
     */
    @ProcessEvent(qname = "{http://wangyy.example.com/food}foodItem")
    public void processFoodItemEvent(EventRequest request, EventResponse response)
    throws PortletException, IOException{
        Event event = request.getEvent();
        FoodItem foodItem = (FoodItem)event.getValue();
        System.out.println("Processing event in EventProcessPortlet id=" + foodItem.getId() + ", name=" + foodItem.getName());
        response.setRenderParameter("foodItemName", foodItem.getName());
    }

猜你喜欢

转载自wangyangqq2008.iteye.com/blog/2043068