I'm using jgroups for cluster node communication. I'm sending messages using channel, but unable to get the received message content. Used msg.getBuffer() and msg.getRawBuffer() methods, but after converting into string getting SOH SOH in the outpout. I just want only the message content not the 'src' or 'dest' hosts. How to get that from Message object?
If you use a string as payload, I suggest either
Set the contents using msg.setObject("hello world") and msg.getObject(), which returns the string "hello world"
OR
Set the contents using msg.setBuffer("hello world".getBytes()) and new String(msg.getRawBuffer(), msg.getOffset(), msg.getLength()).
In the first case, you use a helper method of JGroups to set and retrieve the object, in the latter case you do the (de-)serialization yourself.
Related
I am getting an input json string from a queue to my camel route, I need to unmarshal it to get a java object. After unmarshal I can't access my original message via Exchange object in process. If anyone faced the same issue and found solution, could you please answer this.
I tried to unmarshal a json string to java object from incoming camel route. I wan to get access to original input message after unmarshal.
You can store the original body to an exchange property. Marshal by default replaces the message body but you can use exchange properties to store values for later use in the route.
from("jms:queue:example")
.routeId("receiveExampleMessage")
.convertBodyTo(String.class)
.setProperty("originalBody", body())
.unmarshal(exampleDataFormat)
// Usage:
// Log original body
.log("original body ${exchangeProperty.originalBody}")
// Use exchange property with plain java
.process(ex -> {
String originalBody = ex.getProperty("originalBody",
String.class);
})
// Set property value back to body
.setBody().exchangeProperty("originalBody")
;
I'm trying to create a simple test plan in JMeter to send data over JMS, using a Publisher and a Subscriber. I've got a Java Request which returns a SampleResult object containing a list of subresults, each containing a String ("Hello, world!" + count), and I want to be able to send these Strings over JMS, but I can't work out how to access them.
Everything is working fine in isolation, but how can I plug the result that my Java Request spits out into my Publisher so it can be sent?
Add a Regular Expression Extractor as a child of your Java Request:
You can then use this in your JMS Sampler:
${data}
I am receiving JSON from SNS topic, which I believe is not correct
{
"Type":"Notification",
"MessageId":"message-id-is-this",
"TopicArn":"bouncer.topic.name.here",
"Message":"{\"notificationType\":\"Bounce\",\"bounce\":{\"bounceType\":\"Permanent\",\"bounceSubType\":\"General\",\"bouncedRecipients\":[{\"emailAddress\":\"bounce#simulator.amazonses.com\",\"action\":\"failed\",\"status\":\"5.1.1\",\"diagnosticCode\":\"smtp; 550 5.1.1 user unknown\"}],\"timestamp\":\"2017-04-24T12:58:05.716Z\",\"feedbackId\":\"feedback.id.is.here\",\"remoteMtaIp\":\"192.168.10.1\",\"reportingMTA\":\"dsn; smtp.link.here\"},\"mail\":{\"timestamp\":\"2017-04-24T12:58:05.000Z\",\"source\":\"senderEmail#domainname.com\",\"sourceArn\":\"arn:aws:ses:us-east-1:someid:identity/some#domain.org\",\"sourceIp\":\"127.0.0.1\",\"sendingAccountId\":\"sending.account.id.is.this\",\"messageId\":\"message-id-is-this\",\"destination\":[\"bounce#simulator.amazonses.com\"]}}",
"Timestamp":"2017-04-24T12:58:05.757Z",
"SignatureVersion":"1",
"Signature":"signature.link",
"SigningCertURL":"certificate.link.here",
"UnsubscribeURL":"un.subscribe.link"
}
The problem is with "Message" attribute which instead of holding an object, is referring to string of an object
contains
"Message":"{\"key\":\"value\"}"
instead of
"Message":{"key":"value"}"
hence not mapped to Message class
Temporarily I solved this problem by receiving into string variable and then convert it
private String Message;
private Message objMessage;
and then
Notification noti = toObject(jsonString, Notification.class);
Message msg = toObject(noti.getMessage(), Message.class);
noti.setObjMessage(msg);
for transformation, I am using ObjectMapper.readValue(...)
What is the correct way to solve this problem?
This format is correct.
There are two independent services in the loop, SES and SNS.
The outer structure is an SNS notification -- a generic structure that SNS uses do deliver anything that SNS delivers.
It contains a Message attribute, whose value is always a string, since that is what kind of messages SNS delivers -- strings. Not objects. SNS has no sense of the Message attribute's value being any kind of object. It could be anything, as long as it's valid UTF-8, SNS doesn't care.
To deliver an object as a string, it has to be serialized... and the inner serialization happens to also be JSON.
So Message is nested JSON-in-JSON.
And that's what it's supposed to look like.
When the outer object is serialized, the reserved JSON characters inside must be escaped, as shown here. After the first deserialization, you have exactly what SES sent you -- a JSON string.
You then need to deserialize the resulting string in order to get your object.
I don't think you're doing it wrong. If you are, then I've been doing it wrong for years.
HelloI've built an Android application that uses PubNub to create a chat channel between each user. I would like to be able to identify which users have sent which messages. Currently the login of my app is handled by Parse so each user has a unique username. I found some documentation and example code where rather than sending just the message string, an object was set up that contained the UUID and message string as two different objects that could then be extracted on the subscribe side but from what I could tell this was only in the PubNub javascript code not the Java code for Android.Right now i'm thinking that the only way for me to do this is to attach the UUID/username to the beginning of my message string with a special character to seperate the UUID and the message and then split it up and read it in on the subscribe side. For example String message = "uuidhere_messagehere";. Is this the correct way to approach this or is there a better, more convenient way of doing this?thanks
Correct - PubNub does not inject anything into your messages so you will need to include the sender id within each message that is published. Here's is a simple example of a JSON message you might publish:
{'sender_id':'user_333', 'msg':'this is my msg to you-hoo-hoo'}
Of course, the JSON message can have any key/value pairs you require.
how to convert String to message in java mail api?
You can use the MimeMessage constructor which accepts an InputStream. (See the JavaMail documentation)
Message msg = new MimeMessage(mySession,
new ByteArrayInputStream(myString.getBytes()));
I'm not sure what you're after. Perhaps The Quintessential Program to Send E-Mail [J2EE] helps?
Key method being msg.setText(content).
If the String contains the message body, just take or create a Message object (like a MimeMessage) and use the setTextmethod.
Otherwise, if the String holds a 'full' email, you'll have to separate header and body (from the String) and could use the addHeaderLine() method to recreate a message header.