I have a REST service which receives some data and check the data through an ansynchronous IBM MQ request.
REST controller:
#RestController
#RequestMapping("/request")
public class RequestController {
#RequestMapping(method = RequestMethod.POST)
public Response postRequest(#RequestBody Request request) {
String data = request.getData();
jmsSender.send(data);
// Now I need the response from MQ
// String mqResponse = ...
if (mqIsValid(mqResponse)) {
return createValidResponse();
}
return createNotValidResponse();
}
}
MQ sender:
#Service
public class JmsSender {
public void send(String data) {
jmsTemplate.convertAndSend("QUEUE.TO.MQ", data);
}
}
MQ receiver:
#Component
public class JmsReceiver {
#JmsListener(destination = "QUEUE.FROM.MQ, containerFactory = "DefaultJmsListenerContainerFactory")
public void receiveMessage(String message) {
// How to pass the message to the controller?
}
}
How can I wait for the right data from MQ to create the correct response in the controller?
Is it possible to use a BlockingQueue for this like described here? In my case I have to distinguish the data. I can't just take the first data from the blocking queue.
If for example there are two REST requests simultaneously (with data: abc and xyz). How can I make sure to response the right answer not just the first answer I get from MQ?
I also can't change the MQ interface.
Try using a CountDownLatch like below.
#RestController
#RequestMapping("/request")
public class RequestController {
#RequestMapping(method = RequestMethod.POST)
public Response postRequest(#RequestBody Request request) {
final CountDownLatch jmsLatch = new CountDownLatch (1);
String data = request.getData();
jmsSender.send(data, jmsLatch);
try {
latch.await(); // wait untill latch counted down to 0
} catch (InterruptedException e) {
return createNotValidResponse();
}
return createValidResponse();
}
}
Modify the send method to get the CountDownLatch from controller.
#Service
public class JmsSender {
public void send(String data, final CountDownLatch jmsLatch) {
jmsLatch.await();
jmsTemplate.convertAndSend("QUEUE.TO.MQ", data);
}
}
Modify the receive method to get the same CountDownLatch from controller.
#Component
public class JmsReceiver {
#JmsListener(destination = "QUEUE.FROM.MQ", containerFactory = "DefaultJmsListenerContainerFactory")
public void receiveMessage(String message, final CountDownLatch jmsLatch) {
// Pass the message to the controller
jmsLatch.countDown();
}
}
The trick here is you have to spread the same CountDownLatch instance from controller to the sender and receiver class and call the countDown method after you receive the message.
Since I couldn't find any suitable solution for me, I've created a simple waiting mechanism to get the data.
MqReceiver:
#Component
public class JmsReceiver {
private final Lock lock;
private final Condition containsKey;
private final Map<String, String> responses;
public JmsReceiver() {
this.lock = new ReentrantLock();
this.containsKey = lock.newCondition();
this.responses = new HashMap<>();
}
#JmsListener(destination = "QUEUE.FROM.MQ", containerFactory = "DefaultJmsListenerContainerFactory")
public void receiveMessage(String message) {
put(getKeyFromMessage(message), message);
}
public String get(String key) throws InterruptedException {
lock.lock();
try {
while (!responses.containsKey(key)) {
containsKey.await();
}
return responses.get(key);
} finally {
lock.unlock();
}
}
public void put(String key, String messagee) {
lock.lock();
try {
responses.put(key, messagee);
containsKey.signalAll();
} finally {
lock.unlock();
}
}
}
This can be used in the controller:
#RestController
#RequestMapping("/request")
public class RequestController {
#RequestMapping(method = RequestMethod.POST)
public Response postRequest(#RequestBody Request request) {
String data = request.getData();
jmsSender.send(data);
String key = getKeyFromData(data);
// waits until MQ sends the data
String mqResponse = jmsReceiver.get(key);
if (mqIsValid(mqResponse)) {
return createValidResponse();
}
return createNotValidResponse();
}
}
Solution for scenario sync-async implementing request-reply pattern with jms(activemq)
The idea of this example is working in two different services in a different jvm. The solution is tested concurrent with several instances services:
Service 1 (M1) - Rest api synchronous and in some point start an
async flow using activemq to call the second Service M2 implementing Integration Pattern Request-Reply. You don't need to stop or wait any thread, the jms pattern implements the ack Session.AUTO_ACKNOWLEDGE.
#PostMapping
public AnyDto sendMessage(final AnyDto anyDto) {
return routeService.send(anyDto);
}
public void flowOrchestation (final anyDto data) throws JMSException {
final ObjectMessage objectMessage = composeTemplateMessage(data);
final AnyDto responseDto = jmsMessagingTemplate.convertSendAndReceive(new ActiveMQQueue("queue.request"),
objectMessage, AnyDto.class);
}
private ObjectMessage composeTemplateMessage(final AnyDto data) throws JMSException {
jmsTemplate.setReceiveTimeout(10000L);
jmsMessagingTemplate.setJmsTemplate(jmsTemplate);
Session session = jmsMessagingTemplate.getConnectionFactory().createConnection()
.createSession(false, Session.AUTO_ACKNOWLEDGE);
final ObjectMessage objectMessage = session.createObjectMessage(data);
objectMessage.setJMSCorrelationID(UUID.randomUUID().toString());
objectMessage.setJMSReplyTo(new ActiveMQQueue("queue.response"));
objectMessage.setJMSExpiration(0);
objectMessage.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
return objectMessage;
}
Timeout and expiration can be modified depending your requeriments. 0 expiration means no time to expire.
Service 2 (M2): Just receive the message and make the response to JmsReplyTo setted on M1.
#Component
public class Consumer implements SessionAwareMessageListener<Message> {
#Override
#JmsListener(destination = "${queue.request}")
public void onMessage(Message message, Session session) throws JMSException {
AnyDto anyDto = (AnyDto) ((ActiveMQObjectMessage) message).getObject();
//do some stuff
final ObjectMessage responseMessage = new ActiveMQObjectMessage();
responseMessage.setJMSCorrelationID(message.getJMSCorrelationID());
responseMessage.setObject(dtoModified);
final MessageProducer producer = session.createProducer(message.getJMSReplyTo());
producer.send(responseMessage);
}}
Related
I have a project using spring gRPC. I created a interceptor, when services response succsesfully, the interceptor work fine, but when the service catch an exception, the interceptor not work.
The service:
#GrpcService
public class ClientAppImpl extends ClientAppGrpc.ClientAppImplBase {
#Autowired MyService myService;
#Override
public void myMethod(Request request, StreamObserver<CreateDigitalCertificateResponse> responseObserver) {
try {
Response response = myService.doStuff(request);
responseObserver.onNext(response);
responseObserver.onCompleted();
} catch(Exception ex) {
responseObserver.onError(Status.INTERNAL.withDescription(exception.getMessage()).withCause(exception).asRuntimeException());
}
}
}
The interceptor:
#GrpcGlobalServerInterceptor
public class GrpcServerInterceptor implements ServerInterceptor {
public static final String REQUEST_ID_HEADER = "request-id-bin";
public static final Metadata.Key<byte[]> REQUEST_ID_METADATA_KEY = Metadata.Key.of(REQUEST_ID_HEADER, Metadata.BINARY_BYTE_MARSHALLER);
private static final Map<String, GrpcCall> CALLS = new HashMap<>();
#Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
ForwardingServerCall<ReqT, RespT> responseServerCall = new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
#Override
public void sendMessage(RespT response) {
String callId = new String(headers.get(REQUEST_ID_METADATA_KEY));
GrpcCall grpcCall = CALLS.get(callId);
grpcCall.setResponse(response);
GrpcCallProcessor grpcCallProcessor = new GrpcCallProcessor(grpcCall);
grpcCallProcessor.processCall();
super.sendMessage(response);
}
};
ServerCall.Listener<ReqT> listenerWithContext = Contexts.interceptCall(Context.current(), responseServerCall, headers, next);
return new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(listenerWithContext) {
#Override
public void onMessage(ReqT request) {
String callId = UUID.randomUUID().toString();
headers.put(REQUEST_ID_METADATA_KEY, callId.getBytes());
GrpcCall grpcCall = new GrpcCall();
grpcCall.setCall(call);
grpcCall.setHeaders(headers);
grpcCall.setRequest(request);
CALLS.put(callId, grpcCall);
super.onMessage(request);
}
};
}
}
When the service not catch an exception, the interceptor work fine, and method sendMessage is called. But when the service catch an exception, the method sendMessage is not called.
Is there any way for intercept an exception, and get de request body in the interceptor?
Thanks!
If myService.doStuff() throws an exception, there is not any message being sent. sendMessage() won't be called, but close(Status, Metadata) still will be.
The current usage of headers is broken. Metadata is not thread-safe and you do not know what the current "owner" of the object is doing with it. headers.get(REQUEST_ID_METADATA_KEY) should be performed within interceptCall() directly, before returning.
It isn't entirely clear what the purpose of onMessage() is, but it looks like maybe you should make a copy of the metadata (new Metadata().merge(headers) within interceptCall().
I have two application, one is sending request, another is answering, i'm trying to implement it using #JmsListener.
This code works:
public JmsTemplate jmsTemplate (ConnectionFactory connectionFactory){
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(connectionFactory);
Destination destination = new ActiveMQQueue("replydestination");
jmsTemplate.setDefaultDestination(destination);
return jmsTemplate;
}
#JmsListener(destination = "somedestination",
containerFactory = "defaultJmsListenerContainerFactory")
public void receiveMessage (Message message) throws JMSException {
jmsTemplate.send(new ActiveMQTextMessage());
}
But when change to #SendTo("replydestination") it stops working:
#JmsListener(destination = "somedestination",
containerFactory = "defaultJmsListenerContainerFactory")
#SendTo("replydestination")
public Message receiveMessage (Message message) throws JMSException {
return new ActiveMQTextMessage();
}
Help me to understand why, and can i make this integration without using JmsTemplate.
JMS messages should be constructed with methods from javax.jms.Session or with a builder like this:
#JmsListener(destination = "somedestination",
containerFactory = "defaultJmsListenerContainerFactory")
#SendTo("replydestination")
public org.springframework.messaging.Message<String> listen(javax.jms.Message message) {
org.springframework.messaging.Message<String> reply = MessageBuilder
.withPayload("MyReply")
.build();
return reply;
}
This works too...
#SpringBootApplication
public class So65570932Application {
public static void main(String[] args) {
SpringApplication.run(So65570932Application.class, args);
}
#JmsListener(destination = "foo")
#SendTo("bar")
String listen(String in) {
System.out.println(in);
return in.toUpperCase();
}
#Bean
public ApplicationRunner runner(JmsTemplate template) {
return args -> {
template.convertAndSend("foo", "baz");
template.setReceiveTimeout(10_000);
System.out.println(template.receiveAndConvert("bar"));
};
}
}
I have added custom token based authentication for my spring-web app and extending the same for spring websocket as shown below
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
#Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic", "/queue");
config.setApplicationDestinationPrefixes("/app");
config.setUserDestinationPrefix("/user");
}
#Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/gs-guide-websocket").setAllowedOrigins("*").withSockJS();
}
#Override
public void configureClientInboundChannel(ChannelRegistration registration) {
registration.setInterceptors(new ChannelInterceptorAdapter() {
#Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StompHeaderAccessor accessor =
MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
if (StompCommand.CONNECT.equals(accessor.getCommand())) {
String jwtToken = accessor.getFirstNativeHeader("Auth-Token");
if (!StringUtils.isEmpty(jwtToken)) {
Authentication auth = tokenService.retrieveUserAuthToken(jwtToken);
SecurityContextHolder.getContext().setAuthentication(auth);
accessor.setUser(auth);
//for Auth-Token '12345token' the user name is 'user1' as auth.getName() returns 'user1'
}
}
return message;
}
});
}
}
The client side code to connect to the socket is
var socket = new SockJS('http://localhost:8080/gs-guide-websocket');
stompClient = Stomp.over(socket);
stompClient.connect({'Auth-Token': '12345token'}, function (frame) {
stompClient.subscribe('/user/queue/greetings', function (greeting) {
alert(greeting.body);
});
});
And from my controller I am sending message as
messagingTemplate.convertAndSendToUser("user1", "/queue/greetings", "Hi User1");
For the auth token 12345token the user name is user1. But when I send a message to user1, its not received at the client end. Is there anything I am missing with this?
In your Websocket controller you should do something like this :
#Controller
public class GreetingController {
#Autowired
private SimpMessagingTemplate messagingTemplate;
#MessageMapping("/hello")
public void greeting(Principal principal, HelloMessage message) throws Exception {
Greeting greeting = new Greeting();
greeting.setContent("Hello!");
messagingTemplate.convertAndSendToUser(message.getToUser(), "/queue/reply", greeting);
}
}
On the client side, your user should subscribe to topic /user/queue/reply.
You must also add some destination prefixes :
#Configuration
#EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
#Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic", "/queue" ,"/user");
config.setApplicationDestinationPrefixes("/app");
config.setUserDestinationPrefix("/user");
}
/*...*/
}
When your server receive a message on the /app/hello queue, it should send a message to the user in your dto. User must be equal to the user's principal.
I think the only problem in your code is that your "/user" is not in your destination prefixes. Your greetings messages are blocked because you sent them in a queue that begin with /user and this prefixe is not registered.
You can check the sources at git repo :
https://github.com/simvetanylen/test-spring-websocket
Hope it works!
In my previous project I sent messages to one specific user; in detail I wrote the following:
CLIENT SIDE:
function stompConnect(notificationTmpl)
{
var socket = new SockJS('/comm-svr');
stompClient = Stomp.over(socket);
var theUserId
stompClient.connect({userId:theUserId}, function (frame) {
debug('Connected: ' + frame);
stompClient.subscribe('/topic/connect/'+theUserId, function (data) {
//Handle data
}
});
}
SERVER SIDE
Spring websocket listener:
#Component
public class WebSocketSessionListener
{
private static final Logger logger = LoggerFactory.getLogger(WebSocketSessionListener.class.getName());
private List<String> connectedClientId = new ArrayList<String>();
#EventListener
public void connectionEstablished(SessionConnectedEvent sce)
{
MessageHeaders msgHeaders = sce.getMessage().getHeaders();
Principal princ = (Principal) msgHeaders.get("simpUser");
StompHeaderAccessor sha = StompHeaderAccessor.wrap(sce.getMessage());
List<String> nativeHeaders = sha.getNativeHeader("userId");
if( nativeHeaders != null )
{
String userId = nativeHeaders.get(0);
connectedClientId.add(userId);
if( logger.isDebugEnabled() )
{
logger.debug("Connessione websocket stabilita. ID Utente "+userId);
}
}
else
{
String userId = princ.getName();
connectedClientId.add(userId);
if( logger.isDebugEnabled() )
{
logger.debug("Connessione websocket stabilita. ID Utente "+userId);
}
}
}
#EventListener
public void webSockectDisconnect(SessionDisconnectEvent sde)
{
MessageHeaders msgHeaders = sde.getMessage().getHeaders();
Principal princ = (Principal) msgHeaders.get("simpUser");
StompHeaderAccessor sha = StompHeaderAccessor.wrap(sde.getMessage());
List<String> nativeHeaders = sha.getNativeHeader("userId");
if( nativeHeaders != null )
{
String userId = nativeHeaders.get(0);
connectedClientId.remove(userId);
if( logger.isDebugEnabled() )
{
logger.debug("Connessione websocket stabilita. ID Utente "+userId);
}
}
else
{
String userId = princ.getName();
connectedClientId.remove(userId);
if( logger.isDebugEnabled() )
{
logger.debug("Connessione websocket stabilita. ID Utente "+userId);
}
}
}
public List<String> getConnectedClientId()
{
return connectedClientId;
}
public void setConnectedClientId(List<String> connectedClientId)
{
this.connectedClientId = connectedClientId;
}
}
Spring websocket message sender:
#Autowired
private SimpMessagingTemplate msgTmp;
private void propagateDvcMsg( WebDeviceStatusInfo device )
{
String msg = "";
String userId =((Principal)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getName()
msgTmp.convertAndSend("/topic/connect"+userId, msg);
}
I hope it's useful
We used
org.glassfish.jersey.server.model.ResourceMethod$Builder
to register the method and the handler.
ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod(httpMethod);
methodBuilder.produces(restContext.getProduceContent()).handledBy(inflector);
methodBuilder.consumes(restContext.getConsumeContent()).handledBy(inflector);
The handler class implements the org.glassfish.jersey.process.Inflector<ContainerRequestContext, Response>
public class CommonMethodInflector implements Inflector<ContainerRequestContext, Response>
{
#Override
public Response apply(ContainerRequestContext request)
{
//sync bloc
//using resqest object we do processing in different maner
incRestFeRequestCounters(request.getMethod());
Response response = processIncoming(request);`enter code here`
}
}
Could you please help us in creating the async handler.
our requirement in short:
At runtime only we know the http method and other resources to register.
So, we can not use annotations for resource & httpMethod registration.
We need only programmatic resource registration.
In handler We need the request object so that we can access what method and what json body in it.
we need to make the async response as we are doing huge operation in the processing request.
The first thing you need to do is to suspend the resource method:
ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod(httpMethod)
.suspend(AsyncResponse.NO_TIMEOUT, TimeUnit.Seconds);
Then you have few choices how to process the request in async mode:
"Arbitrary" handler class
builder.addMethod("GET")
// Suspend without time limit.
.suspended(AsyncResponse.NO_TIMEOUT, TimeUnit.SECONDS)
.handledBy(MyHandler.class, MyHandler.class.getMethod("handle", AsyncResponse.class));
and
public class MyHandler {
public void handle(final #Suspended AsyncResponse response) {
Executors.newSingleThreadExecutor().submit(new Runnable() {
#Override
public void run() {
// Simulate long-running operation.
try {
Thread.sleep(1000);
} catch (final InterruptedException ie) {
// NOOP
}
response.resume("Hello World!");
}
});
}
}
Inflector class
Resource.builder("helloworld")
.addMethod("GET")
// Suspend without time limit.
.suspended(AsyncResponse.NO_TIMEOUT, TimeUnit.SECONDS)
// Can be registered only as a class otherwise the
// #Context injection would be out of request scope.
.handledBy(MyAsyncInflector.class);
and
public class MyAsyncInflector implements Inflector<ContainerRequestContext, Response> {
#Context
private AsyncResponse response;
#Override
public Response apply(final ContainerRequestContext context) {
Executors.newSingleThreadExecutor().submit(new Runnable() {
#Override
public void run() {
// Simulate long-running operation.
try {
Thread.sleep(1000);
} catch (final InterruptedException ie) {
// NOOP
}
response.resume("Hello World!");
}
});
return null;
}
}
Annonymous Inflector
Resource.builder("helloworld")
.addMethod("GET")
// Suspend without time limit.
.suspended(AsyncResponse.NO_TIMEOUT, TimeUnit.SECONDS)
.handledBy(new Inflector<ContainerRequestContext, Response>() {
#Inject
private javax.inject.Provider<AsyncResponse> responseProvider;
#Override
public Response apply(final ContainerRequestContext context) {
// Make sure we're in request scope.
final AsyncResponse response = responseProvider.get();
Executors.newSingleThreadExecutor().submit(new Runnable() {
#Override
public void run() {
// Simulate long-running operation.
try {
Thread.sleep(1000);
} catch (final InterruptedException ie) {
// NOOP
}
response.resume("Hello World!");
}
});
return null;
}
});
I am having a problem in sending back a message to a client. Below is my code
JavaScript
dojox.cometd.publish('/service/getservice', {
userid : _USERID,
});
dojox.cometd.subscribe('/service/getservice', function(
message) {
alert("abc");
alert(message.data.test);
});
Configuration Servlet
bayeux.createIfAbsent("/service/getservice", new ConfigurableServerChannel.Initializer() {
#Override
public void configureChannel(ConfigurableServerChannel channel) {
channel.setPersistent(true);
GetListener channelListner = new GetListener();
channel.addListener(channelListner);
}
});
GetListener class
public class GetListener implements MessageListener {
public boolean onMessage(ServerSession ss, ServerChannel sc) {
SomeClassFunction fun = new SomeClassFunction;
}
}
SomeClassFunction
class SomeClassFunction(){
}
here i am creating a boolean variable
boolean success;
if it is true send a message to client which is in javascript. how to send a message back to client. i have tried this line also.
remote.deliver(getServerSession(), "/service/getservice",
message, null);
but it is giving me an error on remote object and getServerSession method.
In order to reach your goal, you don't need to implement listeners nor to configure channels. You may need to add some configuration at a later stage, for example in order to add authorizers.
This is the code for the ConfigurationServlet, taken from this link:
public class ConfigurationServlet extends GenericServlet
{
public void init() throws ServletException
{
// Grab the Bayeux object
BayeuxServer bayeux = (BayeuxServer)getServletContext().getAttribute(BayeuxServer.ATTRIBUTE);
new EchoService(bayeux);
// Create other services here
// This is also the place where you can configure the Bayeux object
// by adding extensions or specifying a SecurityPolicy
}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
throw new ServletException();
}
}
This is the code for EchoService class, taken fro this link:
public class EchoService extends AbstractService
{
public EchoService(BayeuxServer bayeuxServer)
{
super(bayeuxServer, "echo");
addService("/echo", "processEcho");
}
public void processEcho(ServerSession remote, Map<String, Object> data)
{
// if you want to echo the message to the client that sent the message
remote.deliver(getServerSession(), "/echo", data, null);
// if you want to send the message to all the subscribers of the "/myChannel" channel
getBayeux().createIfAbsent("/myChannel");
getBayeux().getChannel("/myChannel").publish(getServerSession(), data, null);
}
}