cancel
Showing results for 
Search instead for 
Did you mean: 

Transaction in the example firstFoundationClient

bardelman
Champ in-the-making
Champ in-the-making
hello,
I'm new alfresco and I started to learn its APIs ..

In the example with the SDK : the firstFoundationClient, I found this snippet in the the project main:

/ / Use to wrap TransactionWork service calls in a user transaction
TransactionService transactionService serviceRegistry.getTransactionService = ();
RetryingTransactionCallback <Object> exampleWork RetryingTransactionCallback <Object> = new ()
{
public Object execute () throws Exception
{
doExample (serviceRegistry);
return null;
}
};
transactionService.getRetryingTransactionHelper (). doInTransaction (exampleWork);

I am very curious and I want to understand that way of writting code (the public Object execute () throws Exception
{…} .. ) written in brackets in an instantiation of a collection (or smthg which looks like ..)
I am very surprised to have a method declaration in a place like this and worse .. in themain!!


I also have another question about transaction: In this example, I don't see where the transaction begins and ends.., is it the injected code which is the method declaration (the public Object execute () ??

Thank you for the help!
1 REPLY 1

jonash
Champ in-the-making
Champ in-the-making
Hi,

This is the Java syntax for an anonymous class.


        RetryingTransactionCallback<Object> exampleWork = new RetryingTransactionCallback<Object>()
        {
            public Object execute() throws Exception
            {
                doExample(serviceRegistry);
                return null;
            }
        };
        transactionService.getRetryingTransactionHelper().doInTransaction(exampleWork);

The code above is a shorthand way of writing  the following:


class ExampleWork implements RetryingTransactionCallback<Object> {
            private ServiceRegistry serviceRegistry;

            public ExampleWork(ServiceRegistry serviceRegistry) {
                        this.serviceRegistry = serviceRegistry;
            }

            // doExample method should be added here

            public Object execute() throws Exception
            {
                doExample(serviceRegistry);
                return null;
            }
}



RetryingTransactionCallback<Object> exampleWork = new ExampleWork(serviceRegistry);
transactionService.getRetryingTransactionHelper().doInTransaction(exampleWork);

As you can see, using an anonymous class makes the code shorter (you don't have to create a whole new class) and—in my opinion—much more readable.

Anonymous inner classes are used a lot in Alfresco to pass a piece of code around. In this case it is used to create a unit of work that is executed in a new transaction using the RetryingTransactionHelper. Here the body of the execute() method is what gets executed in the transaction.

Generics (the <Object> part) are used to parametrize the type of the value that is returned by the unit of work. Generics probably remind you of collections because that's where they are used most often in Java.

I hope this helps.