On Mon, Jun 14, 2010 at 12:26 PM, M.Ismail <[email protected]> wrote:
>
> Although I changed the boolean variable to volatile it only stored its value
> through the flow of a single message (i.e. in the next processor the new
> value is available).However, after I received another request message it
> lost its new value. am I missing something here?
>

I created an unit test which uses a stateful bean in a Camel route
http://svn.apache.org/viewvc?rev=954742&view=rev

> Thanks
>
>
> Claus Ibsen-2 wrote:
>>
>> The boolean in the Login bean should be volatile as you got multiple
>> threads accessing it.
>>
>>
>> On Mon, Jun 14, 2010 at 9:59 AM, M.Ismail <[email protected]> wrote:
>>>
>>> Hi,
>>>
>>> Sorry, I must have confused you with using testing for " because I
>>> couldn't
>>> negate the following predicate: isLogOut".when().method(
>>> "bean:LoginBean?method=isLogOut")
>>> I wanted to make it something like this:
>>> .when().not().method( "bean:LoginBean?method=isLoggedIn")
>>> but I couldn't
>>>
>>> Anyway back to the main problem of the bean not storing its state after
>>> the
>>> message flow is complete.
>>> Here is the complete route:
>>>
>>> from("direct:processMinSatRequests")
>>>        .unmarshal(jaxbUrrApi)
>>>                .process(new UrrXmlReqObjProcessor())
>>>
>>>
>>> .setHeader(MinaConstants.MINA_CLOSE_SESSION_WHEN_COMPLETE,constant(false))
>>>                .choice()
>>>       // Initially i'm logged out so isLogOut returns true until I log in
>>> afterwards it returns false
>>>                .when().method( "bean:LoginBean?method=isLogOut")// We are
>>> going to
>>> attempt to login
>>>                // Attempting to login now
>>>                        .process(new LoginProcessor())
>>>
>>> .to("mina:tcp://"+conf.getIpAddress()+":"+conf.getPortNumber()+"?sync=true&minaLogger=true&textline=true&codec=#MinSatCodec&disconnect=false&lazySessionCreation=false")
>>>                        .convertBodyTo(String.class)
>>>                        // In the following processor I test the result
>>> from
>>> the previous call to the TCP
>>>                        // server. If I logged in successfully then I set
>>> the state of the LogOut in LoginBean
>>>                       // to false in the following processor which is
>>> available below.
>>>                        .process(new ResendBodyProcessor())
>>>
>>> .to("mina:tcp://"+conf.getIpAddress()+":"+conf.getPortNumber()+"?sync=true&minaLogger=true&textline=true&codec=#MinSatCodec&disconnect=false&lazySessionCreation=false")
>>>                        .convertBodyTo(String.class)
>>>                .otherwise()
>>>
>>> .to("mina:tcp://"+conf.getIpAddress()+":"+conf.getPortNumber()+"?sync=true&minaLogger=true&textline=true&codec=#MinSatCodec&disconnect=false&lazySessionCreation=false")
>>>                        .convertBodyTo(String.class)
>>>                .end()
>>>                .process(new RespToApiProcessor())
>>>                .marshal(jaxbUrrApi)
>>>                .process(new UrrXmlEmbedder());
>>>
>>> and here is the ResendBodyProcessor Processor that changes the state of
>>> LogOut static variable in the bean. Do I have to put the bean back to the
>>> registry? If so then how to?:
>>>
>>> public class ResendBodyProcessor implements Processor{
>>>
>>>        public void process(Exchange exchange) throws Exception {
>>>                String response = exchange.getIn().getBody(String.class);
>>>
>>>                // If we failed to login then we should throw an exception
>>>                if(response.contains("RESP:1"))
>>>                {
>>>                        throw new
>>> IllegalInputOrProcessingException("Failed to login to server"+
>>>
>>>      ". Response recieved: "+response);
>>>                }else if(response.contains("RESP:0"))
>>>                {
>>>                        LoginStateBean loginFlag =
>>> (LoginStateBean)exchange.getContext()
>>>
>>>              .getRegistry().lookup("LoginBean");
>>>                        if(loginFlag!=null)
>>>                        {
>>>                                loginFlag.setLogOut(false);
>>>                        }
>>>
>>>
>>>  exchange.getOut().setBody(exchange.getProperty("MessageBody"));
>>>                }else
>>>                        throw new
>>> IllegalInputOrProcessingException("Unknown exception in " +
>>>                                        "ResendBodyProcessor with
>>> response: "+response);
>>>
>>>        }
>>> }
>>>
>>> and the LoginStateBean:
>>>
>>> public class LoginStateBean {
>>>
>>>        private boolean logOut;
>>>
>>>        public LoginStateBean() {
>>>                this.logOut = true;
>>>        }
>>>
>>>
>>>
>>>        public boolean isLogOut() {
>>>                return logOut;
>>>        }
>>>
>>>        public synchronized void setLogOut(boolean logOut) {
>>>                this.logOut = logOut;
>>>        }
>>>
>>> }
>>>
>>>
>>> the thing is that the bean LoginBean doesn't keep the value of "LogOut"
>>> after the message flow is complete.
>>>
>>>
>>> Claus Ibsen-2 wrote:
>>>>
>>>> On Sun, Jun 13, 2010 at 9:57 PM, M.Ismail <[email protected]>
>>>> wrote:
>>>>>
>>>>> Thanks for reply. I am now trying to create a singleton bean in the
>>>>> route
>>>>> that stores the login state for all of the messages (and not just
>>>>> through
>>>>> one message's flow).
>>>>>
>>>>> in camel-context I created this bean:
>>>>>
>>>>> <bean id="LoginBean" class="myPackage.LoginStateBean"
>>>>> scope="singleton">
>>>>>                <property name="logOut" value="true"/>
>>>>>        </bean>
>>>>>
>>>>> and in my route:
>>>>> .when().method( mypackage.LoginStateBean.class, "isLogOut")// We are
>>>>> going
>>>>> to attempt to login
>>>>>
>>>>
>>>> This route must invoke the bean with id LoginBean. So you gotta do
>>>> something like
>>>> .method("LoginBean", "isLogOut")
>>>>
>>>>
>>>>> the only problem is that it doesn't store (cache) its state. May be
>>>>> because
>>>>> there is no way to put the option "cache=true". I thought that by
>>>>> making
>>>>> scope="singleton" it will be enough. But apparently it isn't. All I
>>>>> want
>>>>> to
>>>>> do is make this bean a singleton so that it will store its state. Any
>>>>> suggestions?
>>>>>
>>>>> Thanks,
>>>>>
>>>>>
>>>>> Claus Ibsen-2 wrote:
>>>>>>
>>>>>> On Sat, Jun 12, 2010 at 4:38 PM, M.Ismail <[email protected]>
>>>>>> wrote:
>>>>>>>
>>>>>>> Hi All,
>>>>>>>
>>>>>>> I'm working on an integration with a TCP server and I want to save my
>>>>>>> login
>>>>>>> state (Logged/Not logged) in some variable or in the registry. For
>>>>>>> example,
>>>>>>> whenever I receive a message I may either login if I didn't do that
>>>>>>> before
>>>>>>> and then send the original message or I could simply forward the
>>>>>>> message
>>>>>>> to
>>>>>>> the server if I'm already logged in. I tried exchange.setProperty()
>>>>>>> but
>>>>>>> it
>>>>>>> can only store properties along a single flow i.e. after the message
>>>>>>> is
>>>>>>> sent
>>>>>>> to its destination and another one is received the property is gone!
>>>>>>>
>>>>>>> I'm using camel 2.3 and running it inside the spring container which
>>>>>>> is
>>>>>>> also
>>>>>>> my registry (is it read only? if not could someone tell me how to add
>>>>>>> to
>>>>>>> it
>>>>>>> in the runtime?).
>>>>>>>
>>>>>>> What do you suggest?
>>>>>>>
>>>>>>
>>>>>> A bit like the Claim Check EIP pattern
>>>>>> http://camel.apache.org/claim-check.html
>>>>>>
>>>>>> But in your case just create a bean which you use to store/retrieve
>>>>>> the login state.
>>>>>> Then add that as a spring bean and you can access this bean in the
>>>>>> Camel
>>>>>> routes.
>>>>>>
>>>>>>
>>>>>>
>>>>>>> Thanks
>>>>>>> --
>>>>>>> View this message in context:
>>>>>>> http://old.nabble.com/Saving-Login-state-in-the-route-tp28865247p28865247.html
>>>>>>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>> --
>>>>>> Claus Ibsen
>>>>>> Apache Camel Committer
>>>>>>
>>>>>> Author of Camel in Action: http://www.manning.com/ibsen/
>>>>>> Open Source Integration: http://fusesource.com
>>>>>> Blog: http://davsclaus.blogspot.com/
>>>>>> Twitter: http://twitter.com/davsclaus
>>>>>>
>>>>>>
>>>>>
>>>>> --
>>>>> View this message in context:
>>>>> http://old.nabble.com/Saving-Login-state-in-the-route-tp28865247p28873555.html
>>>>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>>>>
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Claus Ibsen
>>>> Apache Camel Committer
>>>>
>>>> Author of Camel in Action: http://www.manning.com/ibsen/
>>>> Open Source Integration: http://fusesource.com
>>>> Blog: http://davsclaus.blogspot.com/
>>>> Twitter: http://twitter.com/davsclaus
>>>>
>>>>
>>>
>>> --
>>> View this message in context:
>>> http://old.nabble.com/Saving-Login-state-in-the-route-tp28865247p28876829.html
>>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>>
>>>
>>
>>
>>
>> --
>> Claus Ibsen
>> Apache Camel Committer
>>
>> Author of Camel in Action: http://www.manning.com/ibsen/
>> Open Source Integration: http://fusesource.com
>> Blog: http://davsclaus.blogspot.com/
>> Twitter: http://twitter.com/davsclaus
>>
>>
>
> --
> View this message in context: 
> http://old.nabble.com/Saving-Login-state-in-the-route-tp28865247p28877909.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus

Reply via email to