[ 
https://issues.apache.org/jira/browse/LOG4J2-1553?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Gary Gregory updated LOG4J2-1553:
---------------------------------
    Description: 
The class {{AbstractManager}} should implement {{java.lang.AutoCloseable}}.

A manager holds on to resources that must be cleaned up. Typically these 
resources are allocated on instance creation and freed by calling the manager's 
{{release()}} method.

For {{AbstractManager}} to implement {{java.lang.AutoCloseable}}, {{release()}} 
will re-implemented as {{close()}} and deprecated.

There are several benefits to this change:
- Make it obvious and formal that this kind of object must be properly managed.
- A reader or cloner of the code will know that there is a pattern to follow.
- This will make our tests cleaner, smaller and hopefully less error-prone. 

Possible disadvantages:
- Coders may think that managers can be used like resources like a 
{{java.io.File}}.
- Some {{AutoCloseable}} implementations are short-lived, this one not so much, 
so we will add Javadoc for this effect and to note the benefit for unit tests.

Our current unit test code follows this pattern:

{code:java}
SomeManager mgr = new SomeManager(lots, of, params);
try {
    // test code
} finally {
    mgr.close();
}
{code}

Sometimes, we also have:

{code:java}
SomeManager mgr = new SomeManager(lots, of, params);
try {
    // test code
} finally {
    if (mgr != null) {
      mgr.close();
    }
}
{code}

After the proposed change, our test code can look like this:

{code:java}
try (SomeManager mgr = new SomeManager(lots, of, params)) {
    // test code
}
{code}

  was:
The class {{AbstractManager}} should implement {{java.lang.AutoCloseable}}.

A manager holds on to resources that must be cleaned up. Typically these 
resources are allocated on instance creation and freed by calling the manager's 
{{release()}} method.

There are several benefits to this change:
- Make it obvious and formal that this kind of object must be properly managed.
- A reader or cloner of the code will know that there is a pattern to follow.
- This will make our tests cleaner, smaller and hopefully less error-prone. 

Our current unit test code follows this pattern:

{code:java}
SomeManager mgr = new SomeManager(lots, of, params);
try {
    // test code
} finally {
    mgr.close();
}
{code}

Sometimes, we also have:

{code:java}
SomeManager mgr = new SomeManager(lots, of, params);
try {
    // test code
} finally {
    if (mgr != null) {
      mgr.close();
    }
}
{code}

After the proposed change, our test code can look like this:

{code:java}
try (SomeManager mgr = new SomeManager(lots, of, params)) {
    // test code
}
{code}


> AbstractManager should implement AutoCloseable
> ----------------------------------------------
>
>                 Key: LOG4J2-1553
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1553
>             Project: Log4j 2
>          Issue Type: Improvement
>          Components: Appenders
>            Reporter: Gary Gregory
>            Assignee: Gary Gregory
>
> The class {{AbstractManager}} should implement {{java.lang.AutoCloseable}}.
> A manager holds on to resources that must be cleaned up. Typically these 
> resources are allocated on instance creation and freed by calling the 
> manager's {{release()}} method.
> For {{AbstractManager}} to implement {{java.lang.AutoCloseable}}, 
> {{release()}} will re-implemented as {{close()}} and deprecated.
> There are several benefits to this change:
> - Make it obvious and formal that this kind of object must be properly 
> managed.
> - A reader or cloner of the code will know that there is a pattern to follow.
> - This will make our tests cleaner, smaller and hopefully less error-prone. 
> Possible disadvantages:
> - Coders may think that managers can be used like resources like a 
> {{java.io.File}}.
> - Some {{AutoCloseable}} implementations are short-lived, this one not so 
> much, so we will add Javadoc for this effect and to note the benefit for unit 
> tests.
> Our current unit test code follows this pattern:
> {code:java}
> SomeManager mgr = new SomeManager(lots, of, params);
> try {
>     // test code
> } finally {
>     mgr.close();
> }
> {code}
> Sometimes, we also have:
> {code:java}
> SomeManager mgr = new SomeManager(lots, of, params);
> try {
>     // test code
> } finally {
>     if (mgr != null) {
>       mgr.close();
>     }
> }
> {code}
> After the proposed change, our test code can look like this:
> {code:java}
> try (SomeManager mgr = new SomeManager(lots, of, params)) {
>     // test code
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-dev-h...@logging.apache.org

Reply via email to