Liferay 7消息总线

时间:2016-12-06 09:11:03

标签: liferay-7 message-bus

在Liferay 6.2中,我们曾在messaging-spring.xml中配置自定义邮件,并在service.properties中执行enter ..要在Liferay 7 OSGI模块portlet中实现相同功能,任何人都可以指导我如何配置消息总线它?

2 个答案:

答案 0 :(得分:0)

我找到了解决方案。要避免java.lang.IllegalStateException: No servlet context name specified,您需要更改messaging-spring.xml文件中的消息传递配置的实现。

com.liferay.portal.kernel.messaging.config.PluginMessagingConfigurator替换为com.liferay.portal.kernel.messaging.config.DefaultMessagingConfigurator

这对我有用。我能够从一个模块发送消息,并通过另外两个模块接收它。

答案 1 :(得分:0)

我正在与您分享Liferay 7+中Message Bus配置的工作示例

尝试使用我可以正常运行的代码。

AuditMessageBusConfigurator.java

package com.xyz.audit.listner.configurator;

import com.liferay.portal.kernel.concurrent.CallerRunsPolicy;
import com.liferay.portal.kernel.concurrent.RejectedExecutionHandler;
import com.liferay.portal.kernel.concurrent.ThreadPoolExecutor;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.messaging.Destination;
import com.liferay.portal.kernel.messaging.DestinationConfiguration;
import com.liferay.portal.kernel.messaging.DestinationFactory;
import com.liferay.portal.kernel.util.HashMapDictionary;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;

@Component(immediate = true, service = AuditMessageBusConfigurator.class)
public class AuditMessageBusConfigurator {

    public final Log LOGGER = LogFactoryUtil.getLog(AuditMessageBusConfigurator.class);

    private final int _MAXIMUM_QUEUE_SIZE = 1000;

    private volatile BundleContext _bundleContext;

    @Reference
    private DestinationFactory _destinationFactory;

    private final Map<String, ServiceRegistration<Destination>> _serviceRegistrations = new HashMap<>();

    @Activate
    protected void activate(BundleContext context) {
        _bundleContext = context;
        startMessageBus("audit.ptob.destination");
    }

    @Deactivate
    protected void deactivate() {
        for (ServiceRegistration<Destination> serviceRegistration : _serviceRegistrations.values()) {
            destroyMessageBus(serviceRegistration);
        }
        _serviceRegistrations.clear();
    }

    private void destroyMessageBus(ServiceRegistration<Destination> serviceRegistration) {
        Destination destination = _bundleContext.getService(serviceRegistration.getReference());
        serviceRegistration.unregister();
        destination.destroy();
    }

    private void startMessageBus(String busName) {
        Destination destination = createDestination(busName);
        Dictionary<String, Object> propeties = createDictionary(destination);
        createServiceRegistration(destination, propeties);
    }

    private void createServiceRegistration(Destination destination, Dictionary<String, Object> propeties) {
        ServiceRegistration<Destination> messageServiceRegistration = _bundleContext.registerService(Destination.class,
                destination, propeties);
        _serviceRegistrations.put(destination.getName(), messageServiceRegistration);
    }

    private Dictionary<String, Object> createDictionary(Destination destination) {
        Dictionary<String, Object> properties = new HashMapDictionary<>();
        properties.put("destination.name", destination.getName());
        return properties;
    }

    private Destination createDestination(String destinationName) {
        DestinationConfiguration destinationConfiguration = new DestinationConfiguration(
                DestinationConfiguration.DESTINATION_TYPE_PARALLEL, destinationName);

        destinationConfiguration.setMaximumQueueSize(_MAXIMUM_QUEUE_SIZE);

        RejectedExecutionHandler rejectedExecutionHandler = new CallerRunsPolicy() {

            @Override
            public void rejectedExecution(Runnable runnable, ThreadPoolExecutor threadPoolExecutor) {

                if (LOGGER.isWarnEnabled()) {
                    LOGGER.warn("The current thread will handle the request "
                            + "because the audit router's task queue is at " + "its maximum capacity");
                }

                super.rejectedExecution(runnable, threadPoolExecutor);
            }

        };

        destinationConfiguration.setRejectedExecutionHandler(rejectedExecutionHandler);

        return _destinationFactory.createDestination(destinationConfiguration);

    }

}

AuditListnerImpl.java

package com.xyz.audit.listner.impl;

import com.liferay.counter.kernel.service.CounterLocalService;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;
import com.liferay.portal.kernel.util.GetterUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(immediate = true, property = { "destination.name=audit.ptob.destination" }, service = MessageListener.class)
public class AuditListnerImpl implements MessageListener {

    public final Log LOGGER = LogFactoryUtil.getLog(AuditListnerImpl.class);

    @Override
    public void receive(Message message) throws MessageListenerException {
        doReceive(message);
    }

    private void doReceive(Message message) {

        try {
            //DO YOU BUSINESS LOGIC

        } catch (SystemException e) {
            LOGGER.error("SystemException : while audit entry");
        }

    }

}

build.gradle

dependencies {
    compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel"
    compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib"
    compileOnly group: "javax.portlet", name: "portlet-api"
    compileOnly group: "javax.servlet", name: "javax.servlet-api"
    compileOnly group: "jstl", name: "jstl"
    compileOnly group: "org.osgi", name: "osgi.cmpn", version: "6.0.0"


    compileOnly group: "com.liferay", name: "com.liferay.petra.string"
    compileOnly group: "com.liferay", name: "com.liferay.petra.lang"

    compileOnly group: 'org.osgi', name: 'org.osgi.core', version: '6.0.0'
    compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"
    compileOnly group: "com.liferay", name: "com.liferay.osgi.service.tracker.collections", version: "2.0.0"
}