INT-1682, INT-1683 ChatMessageListeningEndpoint and PresenceListeningEndpoint now extend MessageProducerSupport, gaining errorChannel and history writing capabilities

This commit is contained in:
Mark Fisher
2010-12-13 14:00:31 -05:00
parent f5228728e0
commit 228edd8b37
7 changed files with 33 additions and 38 deletions

View File

@@ -58,7 +58,7 @@ public abstract class AbstractXmppInboundChannelAdapterParser extends AbstractSi
"'xmpp-connection' attribute or have default XMPP connection bean registered under the name 'xmppConnection'" +
"(e.g., <int-xmpp:xmpp-connection .../>). If 'id' is not provided the default will be 'xmppConnection'.");
}
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "channel", "requestChannel");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "channel", "outputChannel");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "auto-startup");
this.postProcess(element, parserContext, builder);
}

View File

@@ -17,15 +17,18 @@
package org.springframework.integration.xmpp.core;
import org.jivesoftware.smack.XMPPConnection;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.endpoint.MessageProducerSupport;
import org.springframework.util.Assert;
/**
* @author Oleg Zhurakousky
* @author Mark Fisher
* @since 2.0
*/
public abstract class AbstractXmppConnectionAwareEndpoint extends AbstractEndpoint {
public abstract class AbstractXmppConnectionAwareEndpoint extends MessageProducerSupport {
protected volatile XMPPConnection xmppConnection;
@@ -40,8 +43,18 @@ public abstract class AbstractXmppConnectionAwareEndpoint extends AbstractEndpoi
this.xmppConnection = xmppConnection;
}
/**
* {@link Deprecated} This method will be eligible for removal in 2.1.
* Use {@link #setOutputChannel(MessageChannel)} instead.
*/
@Deprecated
public void setRequestChannel(MessageChannel requestChannel) {
this.setOutputChannel(requestChannel);
}
protected void onInit() throws Exception {
@Override
protected void onInit() {
super.onInit();
BeanFactory beanFactory = this.getBeanFactory();
if (this.xmppConnection == null && beanFactory != null) {
this.xmppConnection = beanFactory.getBean(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, XMPPConnection.class);

View File

@@ -21,8 +21,6 @@ import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Packet;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.xmpp.XmppHeaders;
import org.springframework.integration.xmpp.core.AbstractXmppConnectionAwareEndpoint;
@@ -39,8 +37,6 @@ import org.springframework.util.Assert;
*/
public class ChatMessageListeningEndpoint extends AbstractXmppConnectionAwareEndpoint {
private final MessagingTemplate messagingTemplate = new MessagingTemplate();
private volatile boolean extractPayload = true;
private final PacketListener packetListener = new ChatMessagePublishingPacketListener();
@@ -55,13 +51,6 @@ public class ChatMessageListeningEndpoint extends AbstractXmppConnectionAwareEnd
}
/**
* @param requestChannel the channel on which the inbound message should be sent
*/
public void setRequestChannel(MessageChannel requestChannel) {
this.messagingTemplate.setDefaultChannel(requestChannel);
}
/**
* Specify whether the text message body should be extracted when mapping to a
* Spring Integration Message payload. Otherwise, the full XMPP Message will be
@@ -72,9 +61,8 @@ public class ChatMessageListeningEndpoint extends AbstractXmppConnectionAwareEnd
}
@Override
protected void onInit() throws Exception {
super.onInit();
this.messagingTemplate.afterPropertiesSet();
public String getComponentType() {
return "xmpp:inbound-channel-adapter";
}
@Override
@@ -101,7 +89,7 @@ public class ChatMessageListeningEndpoint extends AbstractXmppConnectionAwareEnd
MessageBuilder<?> messageBuilder = MessageBuilder.withPayload(payload)
.setHeader(XmppHeaders.TYPE, xmppMessage.getType())
.setHeader(XmppHeaders.CHAT, chat);
messagingTemplate.send(messageBuilder.build());
sendMessage(messageBuilder.build());
}
}
}

View File

@@ -20,15 +20,13 @@ import java.util.Collection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Presence;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.xmpp.core.AbstractXmppConnectionAwareEndpoint;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -40,6 +38,7 @@ import org.springframework.util.StringUtils;
*
* @author Josh Long
* @author Oleg Zhurakousky
* @author Mark Fisher
* @since 2.0
*/
public class PresenceListeningEndpoint extends AbstractXmppConnectionAwareEndpoint {
@@ -47,8 +46,6 @@ public class PresenceListeningEndpoint extends AbstractXmppConnectionAwareEndpoi
private static final Log logger = LogFactory.getLog(PresenceListeningEndpoint.class);
private final MessagingTemplate messagingTemplate = new MessagingTemplate();
private final PresencePublishingRosterListener rosterListener = new PresencePublishingRosterListener();
@@ -61,11 +58,9 @@ public class PresenceListeningEndpoint extends AbstractXmppConnectionAwareEndpoi
}
/**
* @param requestChannel the channel on which the inbound message should be sent
*/
public void setRequestChannel(final MessageChannel requestChannel) {
this.messagingTemplate.setDefaultChannel(requestChannel);
@Override
public String getComponentType() {
return "xmpp:presence-inbound-channel-adapter";
}
@Override
@@ -82,12 +77,6 @@ public class PresenceListeningEndpoint extends AbstractXmppConnectionAwareEndpoi
}
}
@Override
protected void onInit() throws Exception {
super.onInit();
this.messagingTemplate.afterPropertiesSet();
}
/**
* RosterListener that subscribes to a given {@link Roster}'s events.
@@ -119,7 +108,7 @@ public class PresenceListeningEndpoint extends AbstractXmppConnectionAwareEndpoi
if (logger.isDebugEnabled()) {
logger.debug("presence changed: " + presence.getFrom() + " - " + presence);
}
messagingTemplate.convertAndSend(presence);
sendMessage(MessageBuilder.withPayload(presence).build());
}
}
}

View File

@@ -46,7 +46,7 @@ public class ChatMessageInboundChannelAdapterParserTests {
public void testInboundAdapter(){
ChatMessageListeningEndpoint adapter = context.getBean("xmppInboundAdapter", ChatMessageListeningEndpoint.class);
assertFalse(adapter.isAutoStartup());
DirectChannel channel = (DirectChannel) TestUtils.getPropertyValue(adapter, "messagingTemplate.defaultChannel");
DirectChannel channel = (DirectChannel) TestUtils.getPropertyValue(adapter, "outputChannel");
assertEquals("xmppInbound", channel.getComponentName());
XMPPConnection connection = (XMPPConnection)TestUtils.getPropertyValue(adapter, "xmppConnection");
assertEquals(connection, context.getBean("testConnection"));

View File

@@ -31,6 +31,7 @@ import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.integration.xmpp.core.XmppContextUtils;
import org.springframework.integration.xmpp.inbound.ChatMessageListeningEndpoint;
@@ -66,6 +67,7 @@ public class ChatMessageListeningEndpointTests {
}).when(connection).removePacketListener(Mockito.any(PacketListener.class));
assertEquals(0, packetListSet.size());
endpoint.setOutputChannel(new QueueChannel());
endpoint.afterPropertiesSet();
endpoint.start();
assertEquals(1, packetListSet.size());
@@ -85,6 +87,7 @@ public class ChatMessageListeningEndpointTests {
bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, mock(XMPPConnection.class));
ChatMessageListeningEndpoint endpoint = new ChatMessageListeningEndpoint();
endpoint.setBeanFactory(bf);
endpoint.setOutputChannel(new QueueChannel());
endpoint.afterPropertiesSet();
assertNotNull(TestUtils.getPropertyValue(endpoint,"xmppConnection"));
}

View File

@@ -68,6 +68,7 @@ public class PresenceListeningEndpointTests {
}
}).when(roster).removeRosterListener(Mockito.any(RosterListener.class));
PresenceListeningEndpoint rosterEndpoint = new PresenceListeningEndpoint(connection);
rosterEndpoint.setOutputChannel(new QueueChannel());
rosterEndpoint.afterPropertiesSet();
assertEquals(0, rosterSet.size());
rosterEndpoint.start();
@@ -89,7 +90,7 @@ public class PresenceListeningEndpointTests {
when(connection.getRoster()).thenReturn(roster);
PresenceListeningEndpoint rosterEndpoint = new PresenceListeningEndpoint(connection);
QueueChannel channel = new QueueChannel();
rosterEndpoint.setRequestChannel(channel);
rosterEndpoint.setOutputChannel(channel);
rosterEndpoint.afterPropertiesSet();
rosterEndpoint.start();
RosterListener rosterListener = (RosterListener) TestUtils.getPropertyValue(rosterEndpoint, "rosterListener");
@@ -105,6 +106,7 @@ public class PresenceListeningEndpointTests {
bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, mock(XMPPConnection.class));
PresenceListeningEndpoint endpoint = new PresenceListeningEndpoint();
endpoint.setBeanFactory(bf);
endpoint.setOutputChannel(new QueueChannel());
endpoint.afterPropertiesSet();
assertNotNull(TestUtils.getPropertyValue(endpoint,"xmppConnection"));
}