Spring AMQP- Receiving Message Object Instead of Content - spring-amqp

I'm using Spring AMQP to send message from one microservice to other one and get response.
I can send message and receive content with this code part:
public void sendAmqpCoreMessage(Message message) throws JsonProcessingException {
List<MyCustomType> response = rabbitTemplate.convertSendAndReceiveAsType("jms.durable.queues", this.queue.getName(), message, new ParameterizedTypeReference<List<MyCustomType>>() {
});
logger.info("Response: {}", new ObjectMapper().writeValueAsString(response));
}
But instead of getting only message content, I need the whole Message object because I'm using some headers which stay in messageProperties of response which's type is Message.
How can I get Message object as response?

Don't use the convert... methods with a Message, just use sendAndReceive().

Related

Why I am unable to send message to the user via chat postMessage api?

We have application that integrates with Slack API and sends the messages via https://api.slack.com/methods/chat.postMessage Slack API. Recently this API started to failing to sent messages to users with error message: method_deprecated. I cannot find the reason why it's deprecated and stopped working in last month.
In order to sent message we use following:
String userId = slackFacade.getUserId(recipientEmail);
String channelId = slackFacade.getDirectChannelId(userId);
slackFacade.postMessage(
Message.builder()
.channel(channelId)
.text(message)
.mrkdwn(true)
.attachment(attachment)
.build());
where slackFacade implementation looks like:
public String getUserId(String email) throws SlackCommunicationException {
return logErrors(slackClient.getUserByEmail(email)).getUser().getId(); //users.lookupByEmail
}
public String getDirectChannelId(String userId) throws SlackCommunicationException {
return logErrors(slackClient.imOpen(userId)).getChannel().getId();
}
I've found the root cause. Actually it was not problem with chat.postMessage but it was part of the changes related to imOpen API:
https://api.slack.com/changelog/2020-01-deprecating-antecedents-to-the-conversations-api

Proper way to NOT send a reply back from webhook

I have setup a webhook and everything is working properly, however iam used to using a long number where STOP is handled automatically, but now I have a short code where we have to handle STOP ourselves. This isnt an issue on sending a message as I can check with my 'blacklist' numbers before sending the message. My question is in the Reply webhook, what is the best way or standard way to NOT send the reply message.
Below is twilios sample code (i added the comment where i would check if they are black listed)
public class SmsController : TwilioController
{
public TwiMLResult Index(SmsRequest incomingMessage)
{
var messagingResponse = new MessagingResponse();
// check for phone number in blacklist to NOT SEND
messagingResponse.Message("The copy cat says: " +
incomingMessage.Body);
return TwiML(messagingResponse);
}
}
If the number is blacklisted and i dont want to send the reply how do I "gracefully" not reply to them as this example takes a TwiMLResult and message response. Do i just set the message to an empty string ? do I return null? Any thoughts ? Thank you !

Old message is not removing in iPhone notification try

I am using push plugin in ionic 3 app, everything is working fine but from my server end we are sending one message at a time and im receiving message to iPhone, but old message should overwrite with new message or once we receive new message then old message is clear automatically..
I'm not able to find anything related to this, please anybody help me to solve this.
const options: PushOptions = {
android: {},
ios: {
alert: 'true',
badge: true,
sound: 'false'
},
windows: {},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('notification').subscribe((notification: any) =>
console.log('Received a notification', notification));
It sounds like you aren't sending your messages configured to be "collapsible". By default each message will be distinct, not overwriting the last message. FCM documents this well:
https://firebase.google.com/docs/cloud-messaging/concept-options
Non-collapsible and collapsible messages
A non-collapsible message
denotes that each individual message is delivered to the device. A
non-collapsible message delivers some useful content, as opposed to a
collapsible message like a content-free "ping" to the mobile app to
contact the server to fetch data.
...
A collapsible message is a message that may be replaced by a new message if it has yet to be delivered to the device.
...
Or if you are not using FCM, here's a reference to Apple's APN docs directly.
To allow the coalescing of similar notifications, you can include a
collapse identifier within a notification request. Normally, when a
device is online, each notification request that you send to APNs
results in a notification delivered to the device. However, when the
apns-collapse-id key is present in your HTTP/2 request header, APNs
coalesces requests whose value for that key is the same. For example,
a news service that sends the same headline twice could use the same
collapse identifier value for both requests. APNs would then coalesce
the two requests into a single notification for delivery to the
device. For details on the apns-collapse-id key
Update with some code details:
public void sendMessage(String title, String body, Map<String, String> data, boolean shouldCollapse) {
PlatformConfiguration platformConfig = new PlatformConfiguration(30);
if (shouldCollapse)
messageBuilder.setAndroidConfig(platformConfig.getCollapsibleAndroidConfig("test")).setApnsConfig(platformConfig.getCollapsibleApnsConfig("test"));
...
public ApnsConfig getCollapsibleApnsConfig(String collapseKey) {
return getCoreApnsConfig().putHeader("apns-collapse-id", collapseKey)
.setAps(getNonCollapsibleApsBuilder().setCategory(collapseKey).setThreadId(collapseKey).build()).build();
}
public AndroidConfig getCollapsibleAndroidConfig(String collapseKey) {
return getCoreAndroidConfig().setCollapseKey(collapseKey)
.setNotification(
getNonCollapsibleAndroidNotificationBuilder().setTag(collapseKey).build()).build();
}

Mirth HL7 ACK ERROR: Message control Ids do not match

I'm starting out with Mirth and HL7 and I'm trying to send a message to a remote server. My MSH looks as follows:
MSH|^~\&|EPIC|EPIC|IMG_SCHEDULE_APPT|REMOTE|20170328193318|PERSONNAME|ORM^O01|12345678|T|2.4||||||||||
The response looks as follows:
MSH|^~\&|IMG_SCHEDULE_APPT|REMOTE|EPIC|EPIC|20170328193318||ACK|12345678|T|2.4|
MSA|AA|||
and I get an error saying ERROR: Message control Ids do not match.
As far as I understand this error means that the Message Control Id which is returned in the ACK message is not the same.
From what I can see, the number 12345678 is the Message Control Id, and I see that number both in the message I send as well as the in the ACK which is returned. So what is wrong here? And who is at fault? Me or the remote party?
Does anybody know how I can solve or debug this?
If you don't want to validate the message control ID, you can open the Response Validation properties and uncheck "Validate Message Control Id":
If the remote system cannot change their logic and you still want to validate the control ID, you can do that in a response transformer:
if (responseStatus == ERROR) {
// msg here is the ACK, origMsg is the encoded data that was sent outbound
var origMsg = new XML(SerializerFactory.getSerializer('HL7V2').toXML(connectorMessage.getEncodedData()));
if (origMsg.MSH['MSH.10']['MSH.10.1'].toString() == msg.MSH['MSH.10']['MSH.10.1'].toString()) {
responseStatus = SENT;
}
}
MSA.2 (Message Control ID) is required and should be the same as the ControlId in the former message that the ACK message acknowleges..

Not able to process response received from template.convertSendAndReceive()

I am trying to send message and receive response using following code
MessageProperties props =MessagePropertiesBuilder.newInstance().setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
.setMessageId("MSG12345").setHeader("type", "type1").setCorrelationId(UUID.randomUUID().toString().getBytes()).build();
Message message = MessageBuilder.withBody(input.getBytes()).andProperties(props).build();
Message response = (Message) template.convertSendAndReceive("key", message);
But, its is throwing ava.lang.ClassCastException: java.lang.String cannot be cast to org.springframework.amqp.core.Message
May be because, I am sending request using java (spring-amqp) program and the receiver is a python (pika) program.
Recevier is sending me a JSON object dumped in string format but I am not able to handle it.
Your problem that you use RabbitTemplate.convertSendAndReceive():
/**
* Basic RPC pattern with conversion. Send a Java object converted to a message to a default exchange with a
* specific routing key and attempt to receive a response, converting that to a Java object. Implementations will
* normally set the reply-to header to an exclusive queue and wait up for some time limited by a timeout.
*
* #param routingKey the routing key
* #param message a message to send
* #return the response if there is one
* #throws AmqpException if there is a problem
*/
Object convertSendAndReceive(String routingKey, Object message) throws AmqpException;
Even if your payload is Message and we we have:
protected Message convertMessageIfNecessary(final Object object) {
if (object instanceof Message) {
return (Message) object;
}
return getRequiredMessageConverter().toMessage(object, new MessageProperties());
}
It converts a reply into a target object from body:
return this.getRequiredMessageConverter().fromMessage(replyMessage);
and don't return Message as you expected.
So, you really have to cast to String and deal with your JSON already on your own.

Resources