如何在 MessageListener 的 receive 方法中获取 PortletRequest 对象

How to get PortletRequest object in receive method of MessageListener

我开发了一个调度程序 portlet,我需要在其中使用 themeDisplay 并获取 groupID。但是,在接收方法中我无法获取 PortletRequest 对象来实例化 themeDisplay 对象。

public class MyScheduler implements MessageListener{
    public void receive(Message message) throws MessageListenerException {  
        ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY);  
        long groupId = themeDisplay.getScopeGroupId();
    }
}

在上面的代码片段中,我应该如何获得 "portletRequest"?

您无法在计划自动触发的消息侦听器中获取 portlet 请求。调度程序事件侦听器完全脱离请求处理。没有要处理的 portlet 请求。

如果您在 portlet 请求处理期间生成事件,则可以在有效负载中传递数据。

异步消息发送示例(例如来自 portlet 操作阶段):

JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
jsonObject.put("userId", themeDisplay.getUserId());
jsonObject.put("groupId", themeDisplay.getScopedGroupId());
...

MessageBusUtil.sendMessage("tour/roadie/setup", jsonObject.toString());

我建议在有效负载中传递单个属性,而不是 PortletRequest 的实例,因为请求在消息处理期间可能无效。

有关详细信息,请参阅 Asynchronous Messaging With Callbacks