In Liferay 6.2 we used to configure Custom mail in messaging-spring.xml and do entery in service.properties.. To achieve the same in Liferay 7 OSGI module portlet, can anyone guide me how could I configure a message bus in it?
Asked
Active
Viewed 1,219 times
2
-
Did you manage to find a solution to this? – Chris M Jun 12 '17 at 11:37
2 Answers
0
I found the solution. To avoid java.lang.IllegalStateException: No servlet context name specified
you need to change implementation of messaging configuration in your messaging-spring.xml
file.
Replace com.liferay.portal.kernel.messaging.config.PluginMessagingConfigurator
with com.liferay.portal.kernel.messaging.config.DefaultMessagingConfigurator
.
This works for me. I was able to send message from one module, and receive it by two other modules.

KirkoR
- 788
- 6
- 13
-
Taka a look here: https://github.com/liferay/liferay-portal/blob/586f66c629b559e79c744559751ecb960218fe0b/modules/apps/collaboration/flags/flags-service/src/main/resources/META-INF/spring/messaging.xml at Liferay source code. Or you can use @Annotations. I will try to write blog post about messaging. – KirkoR Sep 08 '17 at 06:45
0
I am sharing working example with you for Message Bus configuration in Liferay 7+
Try this code which I have working fine.
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"
}

Vishal Shah
- 641
- 6
- 17