Drop RPC-style remoting

Closes gh-27422
This commit is contained in:
Juergen Hoeller
2021-09-17 08:59:58 +02:00
parent 3c8724ba3d
commit 5822f1bf85
79 changed files with 0 additions and 11653 deletions

View File

@@ -1,437 +0,0 @@
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jms.remoting;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageFormatException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TemporaryQueue;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jms.connection.ConnectionFactoryUtils;
import org.springframework.jms.support.JmsUtils;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.SimpleMessageConverter;
import org.springframework.jms.support.destination.DestinationResolver;
import org.springframework.jms.support.destination.DynamicDestinationResolver;
import org.springframework.lang.Nullable;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.remoting.RemoteInvocationFailureException;
import org.springframework.remoting.RemoteTimeoutException;
import org.springframework.remoting.support.DefaultRemoteInvocationFactory;
import org.springframework.remoting.support.RemoteInvocation;
import org.springframework.remoting.support.RemoteInvocationFactory;
import org.springframework.remoting.support.RemoteInvocationResult;
import org.springframework.util.Assert;
/**
* {@link org.aopalliance.intercept.MethodInterceptor} for accessing a
* JMS-based remote service.
*
* <p>Serializes remote invocation objects and deserializes remote invocation
* result objects. Uses Java serialization just like RMI, but with the JMS
* provider as communication infrastructure.
*
* <p>To be configured with a {@link javax.jms.QueueConnectionFactory} and a
* target queue (either as {@link javax.jms.Queue} reference or as queue name).
*
* <p>Thanks to James Strachan for the original prototype that this
* JMS invoker mechanism was inspired by!
*
* @author Juergen Hoeller
* @author James Strachan
* @author Stephane Nicoll
* @since 2.0
* @see #setConnectionFactory
* @see #setQueue
* @see #setQueueName
* @see org.springframework.jms.remoting.JmsInvokerServiceExporter
* @see org.springframework.jms.remoting.JmsInvokerProxyFactoryBean
* @deprecated as of 5.3 (phasing out serialization-based remoting)
*/
@Deprecated
public class JmsInvokerClientInterceptor implements MethodInterceptor, InitializingBean {
@Nullable
private ConnectionFactory connectionFactory;
@Nullable
private Object queue;
private DestinationResolver destinationResolver = new DynamicDestinationResolver();
private RemoteInvocationFactory remoteInvocationFactory = new DefaultRemoteInvocationFactory();
private MessageConverter messageConverter = new SimpleMessageConverter();
private long receiveTimeout = 0;
/**
* Set the QueueConnectionFactory to use for obtaining JMS QueueConnections.
*/
public void setConnectionFactory(@Nullable ConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
/**
* Return the QueueConnectionFactory to use for obtaining JMS QueueConnections.
*/
@Nullable
protected ConnectionFactory getConnectionFactory() {
return this.connectionFactory;
}
/**
* Set the target Queue to send invoker requests to.
*/
public void setQueue(Queue queue) {
this.queue = queue;
}
/**
* Set the name of target queue to send invoker requests to.
* <p>The specified name will be dynamically resolved via the
* {@link #setDestinationResolver DestinationResolver}.
*/
public void setQueueName(String queueName) {
this.queue = queueName;
}
/**
* Set the DestinationResolver that is to be used to resolve Queue
* references for this accessor.
* <p>The default resolver is a {@code DynamicDestinationResolver}. Specify a
* {@code JndiDestinationResolver} for resolving destination names as JNDI locations.
* @see org.springframework.jms.support.destination.DynamicDestinationResolver
* @see org.springframework.jms.support.destination.JndiDestinationResolver
*/
public void setDestinationResolver(@Nullable DestinationResolver destinationResolver) {
this.destinationResolver =
(destinationResolver != null ? destinationResolver : new DynamicDestinationResolver());
}
/**
* Set the {@link RemoteInvocationFactory} to use for this accessor.
* <p>Default is a {@link DefaultRemoteInvocationFactory}.
* <p>A custom invocation factory can add further context information
* to the invocation, for example user credentials.
*/
public void setRemoteInvocationFactory(@Nullable RemoteInvocationFactory remoteInvocationFactory) {
this.remoteInvocationFactory =
(remoteInvocationFactory != null ? remoteInvocationFactory : new DefaultRemoteInvocationFactory());
}
/**
* Specify the {@link MessageConverter} to use for turning
* {@link org.springframework.remoting.support.RemoteInvocation}
* objects into request messages, as well as response messages into
* {@link org.springframework.remoting.support.RemoteInvocationResult} objects.
* <p>Default is a {@link SimpleMessageConverter}, using a standard JMS
* {@link javax.jms.ObjectMessage} for each invocation / invocation result
* object.
* <p>Custom implementations may generally adapt {@link java.io.Serializable}
* objects into special kinds of messages, or might be specifically tailored for
* translating {@code RemoteInvocation(Result)s} into specific kinds of messages.
*/
public void setMessageConverter(@Nullable MessageConverter messageConverter) {
this.messageConverter = (messageConverter != null ? messageConverter : new SimpleMessageConverter());
}
/**
* Set the timeout to use for receiving the response message for a request
* (in milliseconds).
* <p>The default is 0, which indicates a blocking receive without timeout.
* @see javax.jms.MessageConsumer#receive(long)
* @see javax.jms.MessageConsumer#receive()
*/
public void setReceiveTimeout(long receiveTimeout) {
this.receiveTimeout = receiveTimeout;
}
/**
* Return the timeout to use for receiving the response message for a request
* (in milliseconds).
*/
protected long getReceiveTimeout() {
return this.receiveTimeout;
}
@Override
public void afterPropertiesSet() {
if (getConnectionFactory() == null) {
throw new IllegalArgumentException("Property 'connectionFactory' is required");
}
if (this.queue == null) {
throw new IllegalArgumentException("'queue' or 'queueName' is required");
}
}
@Override
@Nullable
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
if (AopUtils.isToStringMethod(methodInvocation.getMethod())) {
return "JMS invoker proxy for queue [" + this.queue + "]";
}
RemoteInvocation invocation = createRemoteInvocation(methodInvocation);
RemoteInvocationResult result;
try {
result = executeRequest(invocation);
}
catch (JMSException ex) {
throw convertJmsInvokerAccessException(ex);
}
try {
return recreateRemoteInvocationResult(result);
}
catch (Throwable ex) {
if (result.hasInvocationTargetException()) {
throw ex;
}
else {
throw new RemoteInvocationFailureException("Invocation of method [" + methodInvocation.getMethod() +
"] failed in JMS invoker remote service at queue [" + this.queue + "]", ex);
}
}
}
/**
* Create a new {@code RemoteInvocation} object for the given AOP method invocation.
* <p>The default implementation delegates to the {@link RemoteInvocationFactory}.
* <p>Can be overridden in subclasses to provide custom {@code RemoteInvocation}
* subclasses, containing additional invocation parameters like user credentials.
* Note that it is preferable to use a custom {@code RemoteInvocationFactory} which
* is a reusable strategy.
* @param methodInvocation the current AOP method invocation
* @return the RemoteInvocation object
* @see RemoteInvocationFactory#createRemoteInvocation
*/
protected RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
return this.remoteInvocationFactory.createRemoteInvocation(methodInvocation);
}
/**
* Execute the given remote invocation, sending an invoker request message
* to this accessor's target queue and waiting for a corresponding response.
* @param invocation the RemoteInvocation to execute
* @return the RemoteInvocationResult object
* @throws JMSException in case of JMS failure
* @see #doExecuteRequest
*/
protected RemoteInvocationResult executeRequest(RemoteInvocation invocation) throws JMSException {
Connection con = createConnection();
Session session = null;
try {
session = createSession(con);
Queue queueToUse = resolveQueue(session);
Message requestMessage = createRequestMessage(session, invocation);
con.start();
Message responseMessage = doExecuteRequest(session, queueToUse, requestMessage);
if (responseMessage != null) {
return extractInvocationResult(responseMessage);
}
else {
return onReceiveTimeout(invocation);
}
}
finally {
JmsUtils.closeSession(session);
ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory(), true);
}
}
/**
* Create a new JMS Connection for this JMS invoker.
*/
protected Connection createConnection() throws JMSException {
ConnectionFactory connectionFactory = getConnectionFactory();
Assert.state(connectionFactory != null, "No ConnectionFactory set");
return connectionFactory.createConnection();
}
/**
* Create a new JMS Session for this JMS invoker.
*/
protected Session createSession(Connection con) throws JMSException {
return con.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
/**
* Resolve this accessor's target queue.
* @param session the current JMS Session
* @return the resolved target Queue
* @throws JMSException if resolution failed
*/
protected Queue resolveQueue(Session session) throws JMSException {
if (this.queue instanceof Queue) {
return (Queue) this.queue;
}
else if (this.queue instanceof String) {
return resolveQueueName(session, (String) this.queue);
}
else {
throw new javax.jms.IllegalStateException(
"Queue object [" + this.queue + "] is neither a [javax.jms.Queue] nor a queue name String");
}
}
/**
* Resolve the given queue name into a JMS {@link javax.jms.Queue},
* via this accessor's {@link DestinationResolver}.
* @param session the current JMS Session
* @param queueName the name of the queue
* @return the located Queue
* @throws JMSException if resolution failed
* @see #setDestinationResolver
*/
protected Queue resolveQueueName(Session session, String queueName) throws JMSException {
return (Queue) this.destinationResolver.resolveDestinationName(session, queueName, false);
}
/**
* Create the invoker request message.
* <p>The default implementation creates a JMS {@link javax.jms.ObjectMessage}
* for the given RemoteInvocation object.
* @param session the current JMS Session
* @param invocation the remote invocation to send
* @return the JMS Message to send
* @throws JMSException if the message could not be created
*/
protected Message createRequestMessage(Session session, RemoteInvocation invocation) throws JMSException {
return this.messageConverter.toMessage(invocation, session);
}
/**
* Actually execute the given request, sending the invoker request message
* to the specified target queue and waiting for a corresponding response.
* <p>The default implementation is based on standard JMS send/receive,
* using a {@link javax.jms.TemporaryQueue} for receiving the response.
* @param session the JMS Session to use
* @param queue the resolved target Queue to send to
* @param requestMessage the JMS Message to send
* @return the RemoteInvocationResult object
* @throws JMSException in case of JMS failure
*/
@Nullable
protected Message doExecuteRequest(Session session, Queue queue, Message requestMessage) throws JMSException {
TemporaryQueue responseQueue = null;
MessageProducer producer = null;
MessageConsumer consumer = null;
try {
responseQueue = session.createTemporaryQueue();
producer = session.createProducer(queue);
consumer = session.createConsumer(responseQueue);
requestMessage.setJMSReplyTo(responseQueue);
producer.send(requestMessage);
long timeout = getReceiveTimeout();
return (timeout > 0 ? consumer.receive(timeout) : consumer.receive());
}
finally {
JmsUtils.closeMessageConsumer(consumer);
JmsUtils.closeMessageProducer(producer);
if (responseQueue != null) {
responseQueue.delete();
}
}
}
/**
* Extract the invocation result from the response message.
* <p>The default implementation expects a JMS {@link javax.jms.ObjectMessage}
* carrying a {@link RemoteInvocationResult} object. If an invalid response
* message is encountered, the {@code onInvalidResponse} callback gets invoked.
* @param responseMessage the response message
* @return the invocation result
* @throws JMSException is thrown if a JMS exception occurs
* @see #onInvalidResponse
*/
protected RemoteInvocationResult extractInvocationResult(Message responseMessage) throws JMSException {
Object content = this.messageConverter.fromMessage(responseMessage);
if (content instanceof RemoteInvocationResult) {
return (RemoteInvocationResult) content;
}
return onInvalidResponse(responseMessage);
}
/**
* Callback that is invoked by {@link #executeRequest} when the receive
* timeout has expired for the specified {@link RemoteInvocation}.
* <p>By default, an {@link RemoteTimeoutException} is thrown. Sub-classes
* can choose to either throw a more dedicated exception or even return
* a default {@link RemoteInvocationResult} as a fallback.
* @param invocation the invocation
* @return a default result when the receive timeout has expired
*/
protected RemoteInvocationResult onReceiveTimeout(RemoteInvocation invocation) {
throw new RemoteTimeoutException("Receive timeout after " + this.receiveTimeout + " ms for " + invocation);
}
/**
* Callback that is invoked by {@link #extractInvocationResult} when
* it encounters an invalid response message.
* <p>The default implementation throws a {@link MessageFormatException}.
* @param responseMessage the invalid response message
* @return an alternative invocation result that should be returned to
* the caller (if desired)
* @throws JMSException if the invalid response should lead to an
* infrastructure exception propagated to the caller
* @see #extractInvocationResult
*/
protected RemoteInvocationResult onInvalidResponse(Message responseMessage) throws JMSException {
throw new MessageFormatException("Invalid response message: " + responseMessage);
}
/**
* Recreate the invocation result contained in the given {@link RemoteInvocationResult}
* object.
* <p>The default implementation calls the default {@code recreate()} method.
* <p>Can be overridden in subclasses to provide custom recreation, potentially
* processing the returned result object.
* @param result the RemoteInvocationResult to recreate
* @return a return value if the invocation result is a successful return
* @throws Throwable if the invocation result is an exception
* @see org.springframework.remoting.support.RemoteInvocationResult#recreate()
*/
@Nullable
protected Object recreateRemoteInvocationResult(RemoteInvocationResult result) throws Throwable {
return result.recreate();
}
/**
* Convert the given JMS invoker access exception to an appropriate
* Spring {@link RemoteAccessException}.
* @param ex the exception to convert
* @return the RemoteAccessException to throw
*/
protected RemoteAccessException convertJmsInvokerAccessException(JMSException ex) {
return new RemoteAccessException("Could not access JMS invoker queue [" + this.queue + "]", ex);
}
}

View File

@@ -1,101 +0,0 @@
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jms.remoting;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* FactoryBean for JMS invoker proxies. Exposes the proxied service for use
* as a bean reference, using the specified service interface.
*
* <p>Serializes remote invocation objects and deserializes remote invocation
* result objects. Uses Java serialization just like RMI, but with the JMS
* provider as communication infrastructure.
*
* <p>To be configured with a {@link javax.jms.QueueConnectionFactory} and a
* target queue (either as {@link javax.jms.Queue} reference or as queue name).
*
* @author Juergen Hoeller
* @since 2.0
* @see #setConnectionFactory
* @see #setQueueName
* @see #setServiceInterface
* @see org.springframework.jms.remoting.JmsInvokerClientInterceptor
* @see org.springframework.jms.remoting.JmsInvokerServiceExporter
* @deprecated as of 5.3 (phasing out serialization-based remoting)
*/
@Deprecated
public class JmsInvokerProxyFactoryBean extends JmsInvokerClientInterceptor
implements FactoryBean<Object>, BeanClassLoaderAware {
@Nullable
private Class<?> serviceInterface;
@Nullable
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
@Nullable
private Object serviceProxy;
/**
* Set the interface that the proxy must implement.
* @param serviceInterface the interface that the proxy must implement
* @throws IllegalArgumentException if the supplied {@code serviceInterface}
* is not an interface type
*/
public void setServiceInterface(Class<?> serviceInterface) {
Assert.notNull(serviceInterface, "'serviceInterface' must not be null");
Assert.isTrue(serviceInterface.isInterface(), "'serviceInterface' must be an interface");
this.serviceInterface = serviceInterface;
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
Assert.notNull(this.serviceInterface, "Property 'serviceInterface' is required");
this.serviceProxy = new ProxyFactory(this.serviceInterface, this).getProxy(this.beanClassLoader);
}
@Override
@Nullable
public Object getObject() {
return this.serviceProxy;
}
@Override
public Class<?> getObjectType() {
return this.serviceInterface;
}
@Override
public boolean isSingleton() {
return true;
}
}

View File

@@ -1,199 +0,0 @@
/*
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jms.remoting;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageFormatException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.jms.support.JmsUtils;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.SimpleMessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.remoting.support.RemoteInvocation;
import org.springframework.remoting.support.RemoteInvocationBasedExporter;
import org.springframework.remoting.support.RemoteInvocationResult;
/**
* JMS message listener that exports the specified service bean as a
* JMS service endpoint, accessible via a JMS invoker proxy.
*
* <p>Note that this class implements Spring's
* {@link org.springframework.jms.listener.SessionAwareMessageListener}
* interface, since it requires access to the active JMS Session.
* Hence, this class can only be used with message listener containers
* which support the SessionAwareMessageListener interface (e.g. Spring's
* {@link org.springframework.jms.listener.DefaultMessageListenerContainer}).
*
* <p>Thanks to James Strachan for the original prototype that this
* JMS invoker mechanism was inspired by!
*
* @author Juergen Hoeller
* @author James Strachan
* @since 2.0
* @see JmsInvokerClientInterceptor
* @see JmsInvokerProxyFactoryBean
* @deprecated as of 5.3 (phasing out serialization-based remoting)
*/
@Deprecated
public class JmsInvokerServiceExporter extends RemoteInvocationBasedExporter
implements SessionAwareMessageListener<Message>, InitializingBean {
private MessageConverter messageConverter = new SimpleMessageConverter();
private boolean ignoreInvalidRequests = true;
@Nullable
private Object proxy;
/**
* Specify the MessageConverter to use for turning request messages into
* {@link org.springframework.remoting.support.RemoteInvocation} objects,
* as well as {@link org.springframework.remoting.support.RemoteInvocationResult}
* objects into response messages.
* <p>Default is a {@link org.springframework.jms.support.converter.SimpleMessageConverter},
* using a standard JMS {@link javax.jms.ObjectMessage} for each invocation /
* invocation result object.
* <p>Custom implementations may generally adapt Serializables into
* special kinds of messages, or might be specifically tailored for
* translating RemoteInvocation(Result)s into specific kinds of messages.
*/
public void setMessageConverter(@Nullable MessageConverter messageConverter) {
this.messageConverter = (messageConverter != null ? messageConverter : new SimpleMessageConverter());
}
/**
* Set whether invalidly formatted messages should be discarded.
* Default is "true".
* <p>Switch this flag to "false" to throw an exception back to the
* listener container. This will typically lead to redelivery of
* the message, which is usually undesirable - since the message
* content will be the same (that is, still invalid).
*/
public void setIgnoreInvalidRequests(boolean ignoreInvalidRequests) {
this.ignoreInvalidRequests = ignoreInvalidRequests;
}
@Override
public void afterPropertiesSet() {
this.proxy = getProxyForService();
}
@Override
public void onMessage(Message requestMessage, Session session) throws JMSException {
RemoteInvocation invocation = readRemoteInvocation(requestMessage);
if (invocation != null) {
RemoteInvocationResult result = invokeAndCreateResult(invocation, this.proxy);
writeRemoteInvocationResult(requestMessage, session, result);
}
}
/**
* Read a RemoteInvocation from the given JMS message.
* @param requestMessage current request message
* @return the RemoteInvocation object (or {@code null}
* in case of an invalid message that will simply be ignored)
* @throws javax.jms.JMSException in case of message access failure
*/
@Nullable
protected RemoteInvocation readRemoteInvocation(Message requestMessage) throws JMSException {
Object content = this.messageConverter.fromMessage(requestMessage);
if (content instanceof RemoteInvocation) {
return (RemoteInvocation) content;
}
return onInvalidRequest(requestMessage);
}
/**
* Send the given RemoteInvocationResult as a JMS message to the originator.
* @param requestMessage current request message
* @param session the JMS Session to use
* @param result the RemoteInvocationResult object
* @throws javax.jms.JMSException if thrown by trying to send the message
*/
protected void writeRemoteInvocationResult(
Message requestMessage, Session session, RemoteInvocationResult result) throws JMSException {
Message response = createResponseMessage(requestMessage, session, result);
MessageProducer producer = session.createProducer(requestMessage.getJMSReplyTo());
try {
producer.send(response);
}
finally {
JmsUtils.closeMessageProducer(producer);
}
}
/**
* Create the invocation result response message.
* <p>The default implementation creates a JMS ObjectMessage for the given
* RemoteInvocationResult object. It sets the response's correlation id
* to the request message's correlation id, if any; otherwise to the
* request message id.
* @param request the original request message
* @param session the JMS session to use
* @param result the invocation result
* @return the message response to send
* @throws javax.jms.JMSException if creating the message failed
*/
protected Message createResponseMessage(Message request, Session session, RemoteInvocationResult result)
throws JMSException {
Message response = this.messageConverter.toMessage(result, session);
String correlation = request.getJMSCorrelationID();
if (correlation == null) {
correlation = request.getJMSMessageID();
}
response.setJMSCorrelationID(correlation);
return response;
}
/**
* Callback that is invoked by {@link #readRemoteInvocation}
* when it encounters an invalid request message.
* <p>The default implementation either discards the invalid message or
* throws a MessageFormatException - according to the "ignoreInvalidRequests"
* flag, which is set to "true" (that is, discard invalid messages) by default.
* @param requestMessage the invalid request message
* @return the RemoteInvocation to expose for the invalid request (typically
* {@code null} in case of an invalid message that will simply be ignored)
* @throws javax.jms.JMSException in case of the invalid request supposed
* to lead to an exception (instead of ignoring it)
* @see #readRemoteInvocation
* @see #setIgnoreInvalidRequests
*/
@Nullable
protected RemoteInvocation onInvalidRequest(Message requestMessage) throws JMSException {
if (this.ignoreInvalidRequests) {
if (logger.isDebugEnabled()) {
logger.debug("Invalid request message will be discarded: " + requestMessage);
}
return null;
}
else {
throw new MessageFormatException("Invalid request message: " + requestMessage);
}
}
}

View File

@@ -1,13 +0,0 @@
/**
* Remoting classes for transparent Java-to-Java remoting via a JMS provider.
*
* <p>Allows the target service to be load-balanced across a number of queue
* receivers, and provides a level of indirection between the client and the
* service: They only need to agree on a queue name and a service interface.
*/
@NonNullApi
@NonNullFields
package org.springframework.jms.remoting;
import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

View File

@@ -1,511 +0,0 @@
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jms.remoting;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Enumeration;
import javax.jms.CompletionListener;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.testfixture.beans.ITestBean;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.SimpleMessageConverter;
import org.springframework.remoting.RemoteTimeoutException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* @author Juergen Hoeller
* @author Stephane Nicoll
*/
class JmsInvokerTests {
private QueueConnectionFactory mockConnectionFactory = mock(QueueConnectionFactory.class);
private QueueConnection mockConnection = mock(QueueConnection.class);
private QueueSession mockSession = mock(QueueSession.class);
private Queue mockQueue = mock(Queue.class);
@BeforeEach
void setUpMocks() throws Exception {
given(mockConnectionFactory.createConnection()).willReturn(mockConnection);
given(mockConnection.createSession(false, Session.AUTO_ACKNOWLEDGE)).willReturn(mockSession);
}
@Test
void jmsInvokerProxyFactoryBeanAndServiceExporter() throws Throwable {
doTestJmsInvokerProxyFactoryBeanAndServiceExporter(false);
}
@Test
void jmsInvokerProxyFactoryBeanAndServiceExporterWithDynamicQueue() throws Throwable {
given(mockSession.createQueue("myQueue")).willReturn(mockQueue);
doTestJmsInvokerProxyFactoryBeanAndServiceExporter(true);
}
@Test
@SuppressWarnings("deprecation")
void receiveTimeoutExpired() {
JmsInvokerProxyFactoryBean pfb = new JmsInvokerProxyFactoryBean() {
@Override
protected Message doExecuteRequest(Session session, Queue queue, Message requestMessage) throws JMSException {
return null; // faking no message received
}
};
pfb.setServiceInterface(ITestBean.class);
pfb.setConnectionFactory(this.mockConnectionFactory);
pfb.setQueue(this.mockQueue);
pfb.setReceiveTimeout(1500);
pfb.afterPropertiesSet();
ITestBean proxy = (ITestBean) pfb.getObject();
assertThatExceptionOfType(RemoteTimeoutException.class).isThrownBy(() ->
proxy.getAge())
.withMessageContaining("1500 ms")
.withMessageContaining("getAge");
}
@SuppressWarnings("deprecation")
private void doTestJmsInvokerProxyFactoryBeanAndServiceExporter(boolean dynamicQueue) throws Throwable {
TestBean target = new TestBean("myname", 99);
final JmsInvokerServiceExporter exporter = new JmsInvokerServiceExporter();
exporter.setServiceInterface(ITestBean.class);
exporter.setService(target);
exporter.setMessageConverter(new MockSimpleMessageConverter());
exporter.afterPropertiesSet();
JmsInvokerProxyFactoryBean pfb = new JmsInvokerProxyFactoryBean() {
@Override
protected Message doExecuteRequest(Session session, Queue queue, Message requestMessage) throws JMSException {
Session mockExporterSession = mock(Session.class);
ResponseStoringProducer mockProducer = new ResponseStoringProducer();
given(mockExporterSession.createProducer(requestMessage.getJMSReplyTo())).willReturn(mockProducer);
exporter.onMessage(requestMessage, mockExporterSession);
assertThat(mockProducer.closed).isTrue();
return mockProducer.response;
}
};
pfb.setServiceInterface(ITestBean.class);
pfb.setConnectionFactory(this.mockConnectionFactory);
if (dynamicQueue) {
pfb.setQueueName("myQueue");
}
else {
pfb.setQueue(this.mockQueue);
}
pfb.setMessageConverter(new MockSimpleMessageConverter());
pfb.afterPropertiesSet();
ITestBean proxy = (ITestBean) pfb.getObject();
assertThat(proxy.getName()).isEqualTo("myname");
assertThat(proxy.getAge()).isEqualTo(99);
proxy.setAge(50);
assertThat(proxy.getAge()).isEqualTo(50);
proxy.setStringArray(new String[] {"str1", "str2"});
assertThat(Arrays.equals(new String[] {"str1", "str2"}, proxy.getStringArray())).isTrue();
assertThatIllegalStateException().isThrownBy(() ->
proxy.exceptional(new IllegalStateException()));
assertThatExceptionOfType(IllegalAccessException.class).isThrownBy(() ->
proxy.exceptional(new IllegalAccessException()));
}
private static class ResponseStoringProducer implements MessageProducer {
Message response;
boolean closed = false;
@Override
public void setDisableMessageID(boolean b) throws JMSException {
}
@Override
public boolean getDisableMessageID() throws JMSException {
return false;
}
@Override
public void setDisableMessageTimestamp(boolean b) throws JMSException {
}
@Override
public boolean getDisableMessageTimestamp() throws JMSException {
return false;
}
@Override
public void setDeliveryMode(int i) throws JMSException {
}
@Override
public int getDeliveryMode() throws JMSException {
return 0;
}
@Override
public void setPriority(int i) throws JMSException {
}
@Override
public int getPriority() throws JMSException {
return 0;
}
@Override
public void setTimeToLive(long l) throws JMSException {
}
@Override
public long getTimeToLive() throws JMSException {
return 0;
}
@Override
public void setDeliveryDelay(long deliveryDelay) throws JMSException {
}
@Override
public long getDeliveryDelay() throws JMSException {
return 0;
}
@Override
public Destination getDestination() throws JMSException {
return null;
}
@Override
public void close() throws JMSException {
this.closed = true;
}
@Override
public void send(Message message) throws JMSException {
this.response = message;
}
@Override
public void send(Message message, int i, int i1, long l) throws JMSException {
}
@Override
public void send(Destination destination, Message message) throws JMSException {
}
@Override
public void send(Destination destination, Message message, int i, int i1, long l) throws JMSException {
}
@Override
public void send(Message message, CompletionListener completionListener) throws JMSException {
}
@Override
public void send(Message message, int deliveryMode, int priority, long timeToLive, CompletionListener completionListener) throws JMSException {
}
@Override
public void send(Destination destination, Message message, CompletionListener completionListener) throws JMSException {
}
@Override
public void send(Destination destination, Message message, int deliveryMode, int priority, long timeToLive, CompletionListener completionListener) throws JMSException {
}
}
private static class MockObjectMessage implements ObjectMessage {
private Serializable serializable;
private Destination replyTo;
public MockObjectMessage(Serializable serializable) {
this.serializable = serializable;
}
@Override
public void setObject(Serializable serializable) throws JMSException {
this.serializable = serializable;
}
@Override
public Serializable getObject() throws JMSException {
return serializable;
}
@Override
public String getJMSMessageID() throws JMSException {
return null;
}
@Override
public void setJMSMessageID(String string) throws JMSException {
}
@Override
public long getJMSTimestamp() throws JMSException {
return 0;
}
@Override
public void setJMSTimestamp(long l) throws JMSException {
}
@Override
public byte[] getJMSCorrelationIDAsBytes() throws JMSException {
return new byte[0];
}
@Override
public void setJMSCorrelationIDAsBytes(byte[] bytes) throws JMSException {
}
@Override
public void setJMSCorrelationID(String string) throws JMSException {
}
@Override
public String getJMSCorrelationID() throws JMSException {
return null;
}
@Override
public Destination getJMSReplyTo() throws JMSException {
return replyTo;
}
@Override
public void setJMSReplyTo(Destination destination) throws JMSException {
this.replyTo = destination;
}
@Override
public Destination getJMSDestination() throws JMSException {
return null;
}
@Override
public void setJMSDestination(Destination destination) throws JMSException {
}
@Override
public int getJMSDeliveryMode() throws JMSException {
return 0;
}
@Override
public void setJMSDeliveryMode(int i) throws JMSException {
}
@Override
public boolean getJMSRedelivered() throws JMSException {
return false;
}
@Override
public void setJMSRedelivered(boolean b) throws JMSException {
}
@Override
public String getJMSType() throws JMSException {
return null;
}
@Override
public void setJMSType(String string) throws JMSException {
}
@Override
public long getJMSExpiration() throws JMSException {
return 0;
}
@Override
public void setJMSExpiration(long l) throws JMSException {
}
@Override
public int getJMSPriority() throws JMSException {
return 0;
}
@Override
public void setJMSPriority(int i) throws JMSException {
}
@Override
public long getJMSDeliveryTime() throws JMSException {
return 0;
}
@Override
public void setJMSDeliveryTime(long deliveryTime) throws JMSException {
}
@Override
public <T> T getBody(Class<T> c) throws JMSException {
return null;
}
@Override
@SuppressWarnings("rawtypes")
public boolean isBodyAssignableTo(Class c) throws JMSException {
return false;
}
@Override
public void clearProperties() throws JMSException {
}
@Override
public boolean propertyExists(String string) throws JMSException {
return false;
}
@Override
public boolean getBooleanProperty(String string) throws JMSException {
return false;
}
@Override
public byte getByteProperty(String string) throws JMSException {
return 0;
}
@Override
public short getShortProperty(String string) throws JMSException {
return 0;
}
@Override
public int getIntProperty(String string) throws JMSException {
return 0;
}
@Override
public long getLongProperty(String string) throws JMSException {
return 0;
}
@Override
public float getFloatProperty(String string) throws JMSException {
return 0;
}
@Override
public double getDoubleProperty(String string) throws JMSException {
return 0;
}
@Override
public String getStringProperty(String string) throws JMSException {
return null;
}
@Override
public Object getObjectProperty(String string) throws JMSException {
return null;
}
@Override
@SuppressWarnings("rawtypes")
public Enumeration getPropertyNames() throws JMSException {
return null;
}
@Override
public void setBooleanProperty(String string, boolean b) throws JMSException {
}
@Override
public void setByteProperty(String string, byte b) throws JMSException {
}
@Override
public void setShortProperty(String string, short i) throws JMSException {
}
@Override
public void setIntProperty(String string, int i) throws JMSException {
}
@Override
public void setLongProperty(String string, long l) throws JMSException {
}
@Override
public void setFloatProperty(String string, float v) throws JMSException {
}
@Override
public void setDoubleProperty(String string, double v) throws JMSException {
}
@Override
public void setStringProperty(String string, String string1) throws JMSException {
}
@Override
public void setObjectProperty(String string, Object object) throws JMSException {
}
@Override
public void acknowledge() throws JMSException {
}
@Override
public void clearBody() throws JMSException {
}
}
private static class MockSimpleMessageConverter extends SimpleMessageConverter {
@Override
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
return new MockObjectMessage((Serializable) object);
}
}
}