B4J Question DB data sync B4J-B4A

udg

Expert
Licensed User
Longtime User
Hi all,
I succesfully experimented along with the guidelines found in this tutorial.
Now, for the real thing, I'm concerned about the buffersize limit. In that same tutorial as an alternative it's suggested to go with a temp file and single-threading the handlers.

My question is, do we have any other better alternative?

What I need to accomplish is a simple data sync at B4A app start-up between the B4J middle-layer and the B4A app. That mens that the webserver reads data from a MySql DB, prepares a list (or other object) and pass it to the B4A app, receiving in exchange new data to write to the DB. This happens a few (very few) times a day with a limited number of users (so the file solution may work well).

Any suggestion? TIA

udg
 

udg

Expert
Licensed User
Longtime User
Thanks all,

@jmon :I did use MQTT before so I should have recalled it..
@Erel : will B4xSerializator take care of the buffersize limit as mentioned in the old tutorial? Can I pass it all the data (whithin "acceptable" limits) returned by my SQL select, no worrying about its overall size?

Do you mind to clean up/correct the following code?
Server sending a list
B4X:
'in Sub Handle(req As ServletRequest, resp As ServletResponse)
...
  Dim tdl As List
  tdl=PrepareTDL
  SendObject(tdl,resp.OutputStream)
...

'send the list to B4A app
Sub SendObject (Obj As Object, Out As OutputStream)
   Dim ser As B4XSerializator
   Dim buffer() As Byte
   buffer=ser.ConvertObjectToBytes(Obj)
   Out.WriteBytes(buffer, 0, buffer.Length)
end sub

B4A client receiving the list
B4X:
'in Sub JobDone (Job As HttpJob)
...
Case "job4"
         Dim out As OutputStream
         out.InitializeToBytesArray(0)
         File.Copy2(Job.GetInputStream, out)
         Dim ar() As Byte
         ar=out.ToBytesArray
         Dim ser As B4XSerializator
         Dim tdl As List = ser.ConvertBytesToObject( ar)

The above works but I'm not sure it's the proper way to do it.
A further question is: when sending more than one object (let's say a list followed by its number of items or a control code), should I simply call SendObject twice, consecutively?

udg
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
will B4xSerializator take care of the buffersize limit as mentioned in the old tutorial? Can I pass it all the data (whithin "acceptable" limits) returned by my SQL select, no worrying about its overall size?
Yes, as long as the complete data can be held in memory.

B4J code:
B4X:
Dim ser As B4XSerializator
Dim buffer() As Byte =ser.ConvertObjectToBytes(Obj)
Out.WriteBytes(buffer, 0, buffer.Length)
B4A code:
B4X:
Dim set As B4XSerializator
Dim tdl As List = ser.ConvertBytesToObject(Bit.InputStreamToBytes(Job.GetInputStream))
 
  • Like
Reactions: udg
Upvote 0

udg

Expert
Licensed User
Longtime User
Thanks a lot. Two more questions:
1. when sending more than one object (let's say a list followed by its number of items or a control code), should I simply call SendObject twice, consecutively?
2. can you briefly comment on a JSON formatted response as an alternative method to exchange info/data between B4J server and B4A client through the Serializator? I can see how a common/shared type makes received data immediately ready and how that in a pure B4x world should suffice as an explanation, but should I take in account other elements before I commit my code to Serializator?

udg
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. You can send multiple objects with:
B4X:
SendObject(Array(Obj1, Obj2), out)
'or
SendObject(CreateMap("obj1": OBJ1, "obj2": OBJ2), out)

2. B4XSerializator doesn't produce JSON. You will need to do it yourself.
Start with B4XSerializator which is simple and efficient (more than JSON) and later add support for JSON if you need it.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi Erel,

I did use JSON before. My question was more about advantages of B4xSerializator over JSON as a way to exchange data between client/server (B4A/B4J) than a request on how to do it. Anyway I read in your response that B4xSerializator is more efficient so I guess that unless my B4A has to talk to a php program rather than my B4J jar, there are no important points in favor of JSON.

Thanks.

udg
 
Upvote 0
Top