How do I send and receive a Java Object from SQS? For instance, I have a java object Log. I send the object to the message queue as
this.getSqs().sendMessage(new SendMessageRequest(myQueueUrl, log.toString());
However, at the time of retrieving the message from queue, I want to be able to retrieve it as List<Log> and use it as a java Log object inside my application. Any pointers on how to do that?
Id use Gson to serialize and deserialize pojo's to strings
you would send the message above as
sendMessage(new SendMessageRequest(myQueueUrl,log.toString());
then when you get a List<Messages> messages = sns.read();
for(Message m:messages){
String json= m.getBody();
Gson g = new Gson();
Log l = g.fromJson(json,Log.class);
}
You have to serialize the message to a string, additionally making sure that all the characters are in the allowed character range.
One way to do it is to use Java serialization (though it's not the best method for serialization, it often works), and then encode the result using e.g. Base64.
For an example see: SoftwareMill common Queue and SoftwareMill common Util
Related
I want to retrieve some contacts with additional info like name and id from native side to Flutter. But List of any Custom Type is not allowed.
Can someone suggest the solution.?
On Android side---
if (methodCall.method.equals("fetch_contacts")) {
List<Contact> contactList = getContacts();
result.success(contactList);
} else {
result.notImplemented();
}
On Flutter Side--
List<dynamic> contactsList = await methodChannel.invokeListMethod('fetch_contacts');
Above android side code is giving IllegalStateException as its can't send Contact model.
Contact is a Java object, so cannot be sent across the method channel. What would it turn into at the Dart end? You could create a Dart class, also called Contact, but even then you couldn't send it. You need to define a way to serialize/deserialize the objects into parts that will traverse the method channel. (See StandardMessageCodec for the basic objects that can cross.)
Here are some possible ways to serialize your object:
JSON - you could turn it into JSON in Java, send a list of Strings across the method channel and then decode those strings into a list of Dart objects. (Or send a single JSON string representing the list of objects.)
Maps - you could turn each object into a map of string-value pairs (the values would need to be of one of the acceptable types - see the link above) and send a list of maps.
I am using CAPL to simulate a test envirmonet for some small tests and i am having problems sending messages or more specific setting up the values.
I am able to read Signal Values with $SignalName, also i am able to set signal values like that.
If i am using this code to send a message the message data is always 0:
on key 't'
{
message MessageName msg;
setSignal(SignalName,i);
write("Value: %d",i);
outport(msg);
}
Witch makes kinda sence becouse i think the message objects are intended to be used to send bytes witch you can access through msg.byte()
I know that i can set signals in messages by msg.SignalName, but again this seems not the right way. I think there should be a way to send a message and all the signals contained in the message are set to the values set by SetSignal() function. Otherwise the SetSignal Funktion is a bit useless
Maybe somebody has an idea.
Thank you
I am using CANalyzer version 8.2 and I do not have the option to use SetSignal(signal, value) function. Setting the signal values by accessing the message selectors seems to be a reasonable approach. However you used the function outport! You need to use the output function to transmit messages.
on key 't' {
message MessageName msg;
msg.signal1 = value1;
output(msg);
}
For this method the database has to be configured so that the message msg contains all the necessary signals (signal1).
If you want to set all signal values to the start values configured in the database use the function:
setSignalStartValues(message msg);
You can set up an interaction layer that will handle the messages as defined in the CAN database (DBC file) assigned to the node. The interaction layer will need some attributes in the database to define how the messages have to be sent. If not already present you may have to add these attributes. If the Tx messages are not sent as expected, check the attributes.
Function output() is useful if you want to implement (and fully control) the sending of the message yourself.
Instead of using SetSignal() it is also possible to write the signal using $SignalName = value;
See this support note:
https://kb.vector.com/upload_551/file/SN-IND-1-011_InteractionLayer(1).pdf
You may have to guess and experiment a bit. In the DBC files provided by a customer I found attribute values that are not mentioned in this document.
I want to obtain a single value from a web service in JSON, just a file name i.e. "picture.png"; based on some parameters passed.
Can IOS (I guess NSJSONSerialization JSONObjectWithData:) handle this single value in JSON or on the server side should I have it send a dictionary key as in {"pic": "picture.gif"}
If there is no picture, I am returning "nopic" so again should I have it return "error" or {"error": "nopic"}
I gather the various JSON specifications are conflicting on this point so my interest is just practical...how best to handle this case.
Thanks for any guidance on this
I have some data that I need to persist through multiple actions within my Grails app. Due to the nature of the data, I would prefer not to store the data in the session. Here is an example of what I would like to do.
class MyController{
def index(){
MyObject object = MyObject.new(params.first, params.second, params.third)
[gspObject:object]
}
def process(){
MyObject object = params.gspObject
//continue from here
}
}
In my GSP if I do
<g:form action="process" params="[gspObject:gspObject]">
Then I get the error
Cannot cast object 'net.package.MyObject#699c14d8' with class 'java.lang.String' to class 'net.package.MyObject'
My question is, If I want to get the object back that I sent to the gsp, how can I get that? Is there some kind of scope that I can save the object in that would be a little safer then session? Is there a way to pass the object into the page itself and pass it back in the next request?
Grails has many layers, but at the bottom you have plain old HTTP just like in any web app. It's a stateless protocol, and you send a text or binary response, and receive text or text + binary requests. But you can't expect to be able to send an arbitrary object to a web browser in HTML and receive it back again in the same state as when you sent it - where is this Java/Groovy JVM object going to be stored in the browser?
You have basically two options. One is to store it at the server, which is less work because it remains as the same object the whole time. The session is a good location because it's coupled to the user, is created on-demand and can automatically time out and be removed, etc. The other is to do what you're trying to do - send it to the client and receive it back - but you are going to have to serialize it from an object (which could be a complex object containing arbitrarily many other objects) and deserialize it from the format you used on the client back into Java/Groovy objects.
JSON is a good option for serialization/marshalling. You could store the stringified object in a hidden form element if your page uses a form, or in a querystring arg if you click a link from this page to the next in the workflow. Don't send all of the object's data though, only what you need to rebuild it. Anything that's available in the database should be referenced by id and reloaded.
Something like
[gspObject: object as JSON]
or
[gspObject: [first: object.first, first: object.firstsecond, ...] as JSON]
will get it in the correct format for sending, and then you can parse the JSON from the request to reinstantiate the instance.
I have a simple iPhone application, and what I am trying to do is the following:
I have a picker view, and I have to fill it by sending a request to servlet and execute a query and send the the result back.
The problem is that the servlet is sending the result as a stream of bytes and I need it as an array.
The question is:
How to send an array from servlet to the application and how to deal with it?
You don't mention whether or not you control the servlet. If you do control it you can format the response in iOS plist format and so directly read the result into a (say) NSArray. If you don't control the servlet, well, you need to understand the format of the data it returns and format it yourself into an NSArray.