You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by tiandike <wh...@21cn.com> on 2007/08/22 10:56:14 UTC

How to implement the synchronized function call?

I want to implement a interface  like   User  getuser(String id);

My ClientApp use getuser  function to get user from ServerApp, 

the client use IoSession.write function to send message from client to
Server, and Server's handler receive message and 
IoSession.write  the user object, then the client handler messageReceived
function called. this is asynchronized.

how can i implement the synchronized function call on mina?
-- 
View this message in context: http://www.nabble.com/How-to-implement-the-synchronized-function-call--tf4310316s16868.html#a12270666
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: How to implement the synchronized function call?

Posted by tiandike <wh...@21cn.com>.
In my project I implement a synclient , to guarantee the response 
correspondence with  request  ,the response and request must has a field
messageID .(I upload my code in syclient.rar)

I use Map and BlockingQueue  in the DefaultConnectionImpl.java

you can write your code like:
ConnectionFactory cf = new DefaultConnectionFactoryImpl();
Connection co = cf.createConnection("localhost",1234);

UICRequestrequest = new UICRequest() 


object res = co.sendandreceive(request);

in your mina server your must get the requestid and put this id in response.
http://www.nabble.com/file/p13761128/syclient.rar syclient.rar 


tiandike wrote:
> 
> I want to implement a interface  like   User  getuser(String id);
> 
> My ClientApp use getuser  function to get user from ServerApp, 
> 
> the client use IoSession.write function to send message from client to
> Server, and Server's handler receive message and 
> IoSession.write  the user object, then the client handler messageReceived
> function called. this is asynchronized.
> 
> how can i implement the synchronized function call on mina?
> 

-- 
View this message in context: http://www.nabble.com/How-to-implement-the-synchronized-function-call--tf4310316s16868.html#a13761128
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: How to implement the synchronized function call?

Posted by tiandike <wh...@21cn.com>.
In my project I implement a synclient , to guarantee the response 
correspondence with  request  ,the response and request must has a field
messageID .

I use Map and BlockingQueue  in the DefaultConnectionImpl.java

you can write your code like:
ConnectionFactory cf = new DefaultConnectionFactoryImpl();
Connection co = cf.createConnection("localhost",1234);

UICRequestrequest = new UICRequest() 


object res = co.sendandreceive(request);

in your mina server your must get the requestid and put this id in response.
http://www.nabble.com/file/p13761128/syclient.rar syclient.rar 


tiandike wrote:
> 
> I want to implement a interface  like   User  getuser(String id);
> 
> My ClientApp use getuser  function to get user from ServerApp, 
> 
> the client use IoSession.write function to send message from client to
> Server, and Server's handler receive message and 
> IoSession.write  the user object, then the client handler messageReceived
> function called. this is asynchronized.
> 
> how can i implement the synchronized function call on mina?
> 

-- 
View this message in context: http://www.nabble.com/How-to-implement-the-synchronized-function-call--tf4310316s16868.html#a13761128
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: How to implement the synchronized function call?

Posted by tiandike <wh...@21cn.com>.
In my project I implement a synclient , to guarantee the response 
correspondence with  request  ,the response and request must has a field
messageID .

I use Map and BlockingQueue  in the DefaultConnectionImpl.java

you can write your code like:
ConnectionFactory cf = new DefaultConnectionFactoryImpl();
Connection co = cf.createConnection("localhost",1234);

YourRequest request = new YourRequest ()
request.setMessageID(1)

object res = co.sendandreceive(request);

in your mina server your must get the requestid and put this id in response.
http://www.nabble.com/file/p13761128/syclient.rar syclient.rar 


tiandike wrote:
> 
> I want to implement a interface  like   User  getuser(String id);
> 
> My ClientApp use getuser  function to get user from ServerApp, 
> 
> the client use IoSession.write function to send message from client to
> Server, and Server's handler receive message and 
> IoSession.write  the user object, then the client handler messageReceived
> function called. this is asynchronized.
> 
> how can i implement the synchronized function call on mina?
> 

-- 
View this message in context: http://www.nabble.com/How-to-implement-the-synchronized-function-call--tf4310316s16868.html#a13761128
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: How to implement the synchronized function call?

Posted by tiandike <wh...@21cn.com>.
In my project I implement a synclient , to guarantee the response 
correspondence with  request  ,the response and request must has a field
messageID .(I upload my code in syclient.rar)

I use Map and BlockingQueue  in the DefaultConnectionImpl.java

you can write your code like:
ConnectionFactory cf = new DefaultConnectionFactoryImpl();
Connection co = cf.createConnection("localhost",1234);

UICRequest request = new UICRequest() 


object res = co.sendandreceive(request);

in your mina server your must get the requestid and put this id in response.
http://www.nabble.com/file/p13761128/syclient.rar syclient.rar 


tiandike wrote:
> 
> I want to implement a interface  like   User  getuser(String id);
> 
> My ClientApp use getuser  function to get user from ServerApp, 
> 
> the client use IoSession.write function to send message from client to
> Server, and Server's handler receive message and 
> IoSession.write  the user object, then the client handler messageReceived
> function called. this is asynchronized.
> 
> how can i implement the synchronized function call on mina?
> 

-- 
View this message in context: http://www.nabble.com/How-to-implement-the-synchronized-function-call--tf4310316s16868.html#a13761128
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: How to implement the synchronized function call?

Posted by Brad Harvey <ha...@gmail.com>.
Hi,

That's because you're ignoring the exception and then continuing on with 
more sends on the same socket.  If your receive times out you need to 
decide what to do with the connection  - the server hasn't responded 
yet, but you don't know why.  It might still respond later if the 
processing is taking longer than your timeout (which is what you're 
seeing here), or you might have some sort of firewall/NAT timeout that 
will cause all future requests to go into the void.  Generally the 
safest approach is to send subsequent requests after a timeout on a new 
connection, but if you don't do this you need a way to match your 
requests and responses and ignore the responses that are for timed out 
requests.

Regards,
Brad.

tiandike wrote:
> If i  use receive with timeout:
>    Object[] res=new Object[num];
>         for(int i=0;i<num;i++){
> 	System.out.println("send: " + i);				
> 	try {
> 	connection.send(i);
> 	res[i]=connection.receive(500);
> 	} catch (Exception e) {
> 					
> 	}
>
> 	System.out.println("echo: " + res[i]);
> 				
> 			}  
>
> I find it will lead a question:
> the result below is :
> send: 0
> echo: null
> send: 1
> echo: echo0
> send: 2
> echo: null
> send: 3
> echo: echo1
> send: 4
> echo: echo2
> send: 5
> echo: null
> send: 6
> echo: echo3
> send: 7      
>
> it leads the request and response not match.
>
>
>
>
> Brad Harvey-2 wrote:
>   
>> Hi Tiandike,
>>
>> Another way of doing it is at 
>> https://issues.apache.org/jira/browse/DIRMINA-375.  I can't promise that 
>> it's better :) 
>>
>> Here's what your Client1 would become (hopefully there aren't too many 
>> mistakes - I'm typing this directly into the email):
>>
>> InetSocketAddress socketAddress = new InetSocketAddress("localhost",
>> 8080);
>> IoServiceConfig config = new SocketConnectorConfig();
>> DefaultIoFilterChainBuilder filterChainBuilder = new 
>> DefaultIoFilterChainBuilder();
>> filterChainBuilder.addLast("codec",  new ProtocolCodecFilter(
>>                                     new
>> SumUpProtocolCodecFactory(false)));
>>
>> config.setFilterChainBuilder(filterChainBuilder);
>>
>> // The ConnectionFactory creates new synchronous connections with the 
>> given address & config.
>> ConnectionFactory cf = new ConnectionFactoryImpl(socketAddress, config);
>> // createConnection does the actual connect (blocking only).
>> NonBlockingConnection connection = cf.createConnection();
>>
>> try {
>>
>>             for (int i = 0; i < values.length; i++) {
>>                  AddMessage m = new AddMessage();
>>                  m.setSequence(i);
>>                  m.setValue(values[i]);
>>                  connection.send(m);
>>                  Object res=connection.receive();           
>>                  if(res instanceof ResultMessage){
>>                      System.out.println("return "+(ResultMessage)res);
>>                  }
>>
>>             }
>> finally {
>>    connection.close();
>> }
>>
>> Regards,
>> Brad.
>>
>> tiandike wrote:
>>     
>>> N http://www.nabble.com/file/p12286978/sumup.rar sumup.rar ow i use
>>> callback
>>> and wait notify  to implement the syn call.
>>> I want to know if there are any better solutions.
>>>
>>> I modify the sumup example to implent syn call.
>>>
>>> the client has three java files :client1.java  ClientSessionHandler1.java 
>>> ClientSupport.java
>>>
>>> my CallBack interface is 
>>> interface CallBack  {
>>> 		
>>> 		void setMessage(Object message);
>>> 		
>>> 	}
>>>
>>> ClientSupport implements CallBack and has  connect  , send and quit
>>> functions
>>>
>>> the send function return the result from server.
>>>
>>> the client1 can use ClientSupport's send function to syn request and get
>>> response from server.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>   
>>>       
>>     
>
>   

Re: How to implement the synchronized function call?

Posted by tiandike <wh...@21cn.com>.
If i  use receive with timeout:
   Object[] res=new Object[num];
        for(int i=0;i<num;i++){
	System.out.println("send: " + i);				
	try {
	connection.send(i);
	res[i]=connection.receive(500);
	} catch (Exception e) {
					
	}

	System.out.println("echo: " + res[i]);
				
			}  

I find it will lead a question:
the result below is :
send: 0
echo: null
send: 1
echo: echo0
send: 2
echo: null
send: 3
echo: echo1
send: 4
echo: echo2
send: 5
echo: null
send: 6
echo: echo3
send: 7      

it leads the request and response not match.




Brad Harvey-2 wrote:
> 
> Hi Tiandike,
> 
> Another way of doing it is at 
> https://issues.apache.org/jira/browse/DIRMINA-375.  I can't promise that 
> it's better :) 
> 
> Here's what your Client1 would become (hopefully there aren't too many 
> mistakes - I'm typing this directly into the email):
> 
> InetSocketAddress socketAddress = new InetSocketAddress("localhost",
> 8080);
> IoServiceConfig config = new SocketConnectorConfig();
> DefaultIoFilterChainBuilder filterChainBuilder = new 
> DefaultIoFilterChainBuilder();
> filterChainBuilder.addLast("codec",  new ProtocolCodecFilter(
>                                     new
> SumUpProtocolCodecFactory(false)));
> 
> config.setFilterChainBuilder(filterChainBuilder);
> 
> // The ConnectionFactory creates new synchronous connections with the 
> given address & config.
> ConnectionFactory cf = new ConnectionFactoryImpl(socketAddress, config);
> // createConnection does the actual connect (blocking only).
> NonBlockingConnection connection = cf.createConnection();
> 
> try {
> 
>             for (int i = 0; i < values.length; i++) {
>                  AddMessage m = new AddMessage();
>                  m.setSequence(i);
>                  m.setValue(values[i]);
>                  connection.send(m);
>                  Object res=connection.receive();           
>                  if(res instanceof ResultMessage){
>                      System.out.println("return "+(ResultMessage)res);
>                  }
> 
>             }
> finally {
>    connection.close();
> }
> 
> Regards,
> Brad.
> 
> tiandike wrote:
>> N http://www.nabble.com/file/p12286978/sumup.rar sumup.rar ow i use
>> callback
>> and wait notify  to implement the syn call.
>> I want to know if there are any better solutions.
>>
>> I modify the sumup example to implent syn call.
>>
>> the client has three java files :client1.java  ClientSessionHandler1.java 
>> ClientSupport.java
>>
>> my CallBack interface is 
>> interface CallBack  {
>> 		
>> 		void setMessage(Object message);
>> 		
>> 	}
>>
>> ClientSupport implements CallBack and has  connect  , send and quit
>> functions
>>
>> the send function return the result from server.
>>
>> the client1 can use ClientSupport's send function to syn request and get
>> response from server.
>>
>>
>>
>>
>>
>>
>>
>>
>>   
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-implement-the-synchronized-function-call--tf4310316s16868.html#a13502527
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: How to implement the synchronized function call?

Posted by Brad Harvey <ha...@gmail.com>.
Hi Tiandike,

Another way of doing it is at 
https://issues.apache.org/jira/browse/DIRMINA-375.  I can't promise that 
it's better :) 

Here's what your Client1 would become (hopefully there aren't too many 
mistakes - I'm typing this directly into the email):

InetSocketAddress socketAddress = new InetSocketAddress("localhost", 8080);
IoServiceConfig config = new SocketConnectorConfig();
DefaultIoFilterChainBuilder filterChainBuilder = new 
DefaultIoFilterChainBuilder();
filterChainBuilder.addLast("codec",  new ProtocolCodecFilter(
                                    new SumUpProtocolCodecFactory(false)));

config.setFilterChainBuilder(filterChainBuilder);

// The ConnectionFactory creates new synchronous connections with the 
given address & config.
ConnectionFactory cf = new ConnectionFactoryImpl(socketAddress, config);
// createConnection does the actual connect (blocking only).
NonBlockingConnection connection = cf.createConnection();

try {

            for (int i = 0; i < values.length; i++) {
                 AddMessage m = new AddMessage();
                 m.setSequence(i);
                 m.setValue(values[i]);
                 connection.send(m);
                 Object res=connection.receive();           
                 if(res instanceof ResultMessage){
                     System.out.println("return "+(ResultMessage)res);
                 }

            }
finally {
   connection.close();
}

Regards,
Brad.

tiandike wrote:
> N http://www.nabble.com/file/p12286978/sumup.rar sumup.rar ow i use callback
> and wait notify  to implement the syn call.
> I want to know if there are any better solutions.
>
> I modify the sumup example to implent syn call.
>
> the client has three java files :client1.java  ClientSessionHandler1.java 
> ClientSupport.java
>
> my CallBack interface is 
> interface CallBack  {
> 		
> 		void setMessage(Object message);
> 		
> 	}
>
> ClientSupport implements CallBack and has  connect  , send and quit
> functions
>
> the send function return the result from server.
>
> the client1 can use ClientSupport's send function to syn request and get
> response from server.
>
>
>
>
>
>
>
>
>   

Re: How to implement the synchronized function call?

Posted by tiandike <wh...@21cn.com>.
N http://www.nabble.com/file/p12286978/sumup.rar sumup.rar ow i use callback
and wait notify  to implement the syn call.
I want to know if there are any better solutions.

I modify the sumup example to implent syn call.

the client has three java files :client1.java  ClientSessionHandler1.java 
ClientSupport.java

my CallBack interface is 
interface CallBack  {
		
		void setMessage(Object message);
		
	}

ClientSupport implements CallBack and has  connect  , send and quit
functions

the send function return the result from server.

the client1 can use ClientSupport's send function to syn request and get
response from server.








-- 
View this message in context: http://www.nabble.com/How-to-implement-the-synchronized-function-call--tf4310316s16868.html#a12286978
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.