How to disable retry in spring boot without removing it? - spring-boot

i have 2 methods in class:
#Retryable(maxAttemptsExpression="${retry.maxAttempts}", backoff=#Backoff(delayExpression = "${retry.delay}", multiplierExpression = "${retry.multiplier}"))
public void foo() {
/**some body**/
}
#Recover
public void fooRecover() {
/**some body**/
}
In some cases i need to disable retrying, but maxAttempts can't be equals zero, so i can't simply do it. So how to correctly disable retrying in some cases?

#Retryable annotation has a property exceptionExpression where you can specify SpEL expression that evaluates to true or false.
/**
* Specify an expression to be evaluated after the
* {#code SimpleRetryPolicy.canRetry()} returns true - can be used to conditionally
* suppress the retry. Only invoked after an exception is thrown. The root object for
* the evaluation is the last {#code Throwable}. Other beans in the context can be
* referenced. For example: <pre class=code>
* {#code "message.contains('you can retry this')"}.
* </pre> and <pre class=code>
* {#code "#someBean.shouldRetry(#root)"}.
* </pre>
* #return the expression.
* #since 1.2
*/
String exceptionExpression() default "";
So if you want to disable retry you can inject boolean string parameter with value "false"
#Retryable(exceptionExpression = "${retry.shouldRetry}", ...other stuff...)

I think the problems are on the conditions. In which case you want to retry and which you want to stop?
So then you can define the value=?Exception.class in the annotation.
For example:
#Retryable(value=MUST_RETRY_EXCEPTION.class)
public void foo() {
if (condition) {
throw new MUST_RETRY_EXCEPTION(...);
}
if (condition2) {
throw new NO_RETRY_EXCEPTION(...);
}
}

remove #Retryable, you simply don't want what code dose then remove it

Related

Walk all results using a Spring Data and pagination

I'm calling a paginate service
I can walk all the paginated collection using code like this:
Pageable pageable = PageRequest.of(0,100);
Page<Event> events = eventsService.getEventsPage(pageable);
while(!events.isLast()) {
doStuff(events)
events = eventsService.getEventsPage(events.nextPageable())
}
doStuff(events) // rest of events
is there a more elegant solution?. I need the pagination because otherwise, the system will run out of memory.
I'd use the getContent() from Slice (which is inherited by Page).
List <T> getContent()
Returns the page content as List.
Pageable pageable = PageRequest.of(0,100);
Page<Event> events = eventsService.getEventsPage(pageable);
List<Event> eventList = events.getContent();
for (Event event : eventList) {
// do stuff
}
I feel like having to state the service call and doStuff twice is somehow fragile. You can utilze the fact that nextPageable() will give you the unpaged singleton if the current page is the last one already.
Pageable pageable = PageRequest.of(0,100);
do {
Page<Event> events = eventsService.getEventsPage(pageable);
doStuff(events)
pageable = events.nextPageable();
} while(pageable.isPaged())
Here is the doc of nextPageable().
/**
* Returns the {#link Pageable} to request the next {#link Slice}. Can be {#link Pageable#unpaged()} in case the
* current {#link Slice} is already the last one. Clients should check {#link #hasNext()} before calling this method.
*
* #return
* #see #nextOrLastPageable()
*/
Pageable nextPageable();
Pageable#unpaged() will return a singleton that returns false for isPaged().

Cannot create async method which return ElementArrayFinder for E2E tests

I have a class where I want to make a async method which will return ElementArrayFinder. I need exactly this type, not ElementFinder[] because my waiters based on it and I can wait element which isn't present now in DOM. Below you can find a simple example of the structure. At row 'return this.collection;' i have an error:
[ts]
Type 'any[]' is not assignable to type 'ElementArrayFinder'.
Property 'browser_' is missing in type 'any[]'.
I tried to cast result in a different way and tried to use Promise.resolve, but no success. Could somebody help me with this case?
export class Test {
private grid1: ElementFinder;
private grid2: ElementFinder;
get collection1(): ElementArrayFinder {
return this.grid1
.element(by.css('tbody'))
.all(by.tagName('tr'));
}
get collection2(): ElementArrayFinder {
return this.grid2
.element(by.css('tbody'))
.all(by.tagName('tr'));
}
public async getCollection(): Promise<ElementArrayFinder> {
if (await this.collection.count() === 0) {
return this.collection1;
}
return this.collection2;
}
}
Bug report
Node Version: v8.2.1
Protractor Version: 5.2.0
I found the answer in file element.d.ts file.
/**
* Retrieve the elements represented by the ElementArrayFinder. The input
* function is passed to the resulting promise, which resolves to an
* array of ElementFinders.
*
* #alias element.all(locator).then(thenFunction)
* #view
* <ul class="items">
* <li>First</li>
* <li>Second</li>
* <li>Third</li>
* </ul>
*
* #example
* element.all(by.css('.items li')).then(function(arr) {
* expect(arr.length).toEqual(3);
* });
*
* // Or using the shortcut $$() notation instead of element.all(by.css()):
*
* $$('.items li').then(function(arr) {
* expect(arr.length).toEqual(3);
* });
*
* #param {function(Array.<ElementFinder>)} fn
* #param {function(Error)} errorFn
*
* #returns {!webdriver.promise.Promise} A promise which will resolve to
* an array of ElementFinders represented by the ElementArrayFinder.
*/
then<T>(fn?: (value: ElementFinder[] | any[]) => T | wdpromise.IThenable<T>, errorFn?: (error: any) => any): wdpromise.Promise<T>;
Based on this documentation ElementArrayFinder type is resolved by .then to the ElementFinder[]. That's why I cannot create method which return ElementArrayFinder type in promise, but can create method which simply return ElementArrayFinder type.

Java Optional why not an ifNotPresent method?

I was wondering why on the Java8 API the Optional class have the method ifPresent(Consumer< T> consumer) and not the ifNotPresent(Runnable** consumer)?
What's the intent of the API? Isn't it to emulate the functional pattern matching?
** Java doesn't have a zero argument void functional interface...
As Misha said, this feature will come with jdk9 in the form of a ifPresentOrElse method.
/**
* If a value is present, performs the given action with the value,
* otherwise performs the given empty-based action.
*
* #param action the action to be performed, if a value is present
* #param emptyAction the empty-based action to be performed, if no value is
* present
* #throws NullPointerException if a value is present and the given action
* is {#code null}, or no value is present and the given empty-based
* action is {#code null}.
* #since 9
*/
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) {
if (value != null) {
action.accept(value);
} else {
emptyAction.run();
}
}

symfony 1.4 - doctrine save() method: how to identify success

Can someone please tell me how to know if the save() method was successful or not. I tried to find out if it returned true/false, but it returns null.
for example:
$contact_obj->save();
How to know that it worked?
If you use Doctrine, try trySave():
abstract class Doctrine_Record {
...
/**
* tries to save the object and all its related components.
* In contrast to Doctrine_Record::save(), this method does not
* throw an exception when validation fails but returns TRUE on
* success or FALSE on failure.
*
* #param Doctrine_Connection $conn optional connection parameter
* #return TRUE if the record was saved sucessfully without errors, FALSE otherwise.
*/
public function trySave(Doctrine_Connection $conn = null) {
try {
$this->save($conn);
return true;
} catch (Doctrine_Validator_Exception $ignored) {
return false;
}
}
}
you can check by $contact_obj->getId() function, it will return the inserted id on success.

Is there a function like _compile_select or get_compiled_select()?

Looks like _compile_select is deprecated and get_compiled_select is not added to 2.1.0. Are there any other functions like those two? And also I am curious. Is there any particular reason to not adding get_compiled_select() to Active Record and removing _compile_select?
I've added get_compiled_select() to DB_active_rec.php and it seems to work without problem, but i wouldn't remove _compile_select() since it's used in many other methods.
The pull request for adding this method is here, with some other useful methods like:
get_compiled_select()
get_compiled_insert()
get_compiled_update()
get_compiled_delete()
https://github.com/EllisLab/CodeIgniter/pull/307
if you want just the method, it's just this:
/**
* Get SELECT query string
*
* Compiles a SELECT query string and returns the sql.
*
* #access public
* #param string the table name to select from (optional)
* #param boolean TRUE: resets AR values; FALSE: leave AR vaules alone
* #return string
*/
public function get_compiled_select($table = '', $reset = TRUE)
{
if ($table != '')
{
$this->_track_aliases($table);
$this->from($table);
}
$select = $this->_compile_select();
if ($reset === TRUE)
{
$this->_reset_select();
}
return $select;
}

Resources