2

在 Liferay 6.2 中,我们曾经在 messing-spring.xml 中配置自定义邮件并在 service.properties 中进行输入。为了在 Liferay 7 OSGI 模块 portlet 中实现相同的功能,谁能指导我如何在其中配置消息总线?

4

2 回答 2

0

我正在与您分享 Liferay 7+ 中消息总线配置的工作示例

试试这个我工作正常的代码。

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");
        }

    }

}

构建.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"
}
于 2020-06-16T07:38:40.003 回答
0

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

替换com.liferay.portal.kernel.messaging.config.PluginMessagingConfiguratorcom.liferay.portal.kernel.messaging.config.DefaultMessagingConfigurator

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

于 2017-07-06T10:01:22.500 回答