Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from OpenWiseSolutions/feature/OHFJIRA-6-final…
…-messages [OHFJIRA-6] : messages in final state
- Loading branch information
Showing
21 changed files
with
1,224 additions
and
15 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...main/java/org/openhubframework/openhub/api/asynch/finalmessage/FinalMessageProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.openhubframework.openhub.api.asynch.finalmessage; | ||
|
||
import org.openhubframework.openhub.api.entity.Message; | ||
import org.springframework.core.Ordered; | ||
|
||
/** | ||
* Contract for final messages processor. | ||
* Messages that are no-longer considered relevant for openhub async workflow are expected | ||
* to be processed by this processor. | ||
* | ||
* There can be multiple processors, that are invoked in order defined in getOrder method. | ||
* | ||
* All processor are expected to be executed in transaction, if any should throw | ||
* an exception, transaction will be rolled back. | ||
* | ||
* @author Karel Kovarik | ||
* @since 2.1 | ||
*/ | ||
@FunctionalInterface | ||
public interface FinalMessageProcessor extends Ordered { | ||
|
||
/** | ||
* Default order, if {@link FinalMessageProcessor#getOrder()} method is not implemented. | ||
*/ | ||
int DEFAULT_ORDER = 0; | ||
|
||
/** | ||
* Process message in OpenHub datastore. | ||
* | ||
* @param message the message to be processed. | ||
*/ | ||
void processMessage(Message message); | ||
|
||
/** | ||
* Get the order for processor. | ||
* | ||
* @return the order of processor, lower value is expected to be invoked first. | ||
*/ | ||
default int getOrder() { | ||
return DEFAULT_ORDER; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
.../org/openhubframework/openhub/api/asynch/finalmessage/FinalMessagesProcessingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.openhubframework.openhub.api.asynch.finalmessage; | ||
|
||
/** | ||
* Service to find final messages that are relevant for processing and | ||
* invoke all {@link FinalMessageProcessor} beans on them. | ||
* | ||
* @author Karel Kovarik | ||
* @since 2.1 | ||
*/ | ||
@FunctionalInterface | ||
public interface FinalMessagesProcessingService { | ||
|
||
/** | ||
* Do trigger messages processing. | ||
*/ | ||
void processMessages(); | ||
} |
4 changes: 4 additions & 0 deletions
4
...-api/src/main/java/org/openhubframework/openhub/api/asynch/finalmessage/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* Final messages in async workflow handling. | ||
*/ | ||
package org.openhubframework.openhub.api.asynch.finalmessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...enhubframework/openhub/core/common/asynch/finalmessage/AbstractFinalMessageProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.openhubframework.openhub.core.common.asynch.finalmessage; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
import org.openhubframework.openhub.api.asynch.finalmessage.FinalMessageProcessor; | ||
import org.openhubframework.openhub.api.entity.Message; | ||
import org.openhubframework.openhub.core.common.dao.MessageDao; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.transaction.TransactionDefinition; | ||
import org.springframework.transaction.TransactionStatus; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
/** | ||
* Abstract class for custom implementations of {@link FinalMessageProcessor}. | ||
* | ||
* @author Karel Kovarik | ||
* @since 2.1 | ||
*/ | ||
public abstract class AbstractFinalMessageProcessor implements FinalMessageProcessor { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(AbstractFinalMessageProcessor.class); | ||
|
||
@Autowired | ||
protected MessageDao messageDao; | ||
|
||
// transactionTemplate | ||
@Autowired | ||
protected TransactionTemplate transactionTemplate; | ||
|
||
@Autowired | ||
protected PlatformTransactionManager transactionManager; | ||
|
||
@PostConstruct | ||
void initializeTransactionManager() { | ||
this.transactionTemplate = new TransactionTemplate(transactionManager); | ||
this.transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); | ||
} | ||
|
||
@Override | ||
public final void processMessage(final Message message) { | ||
LOG.trace("Will process message with id [{}].", message.getId()); | ||
|
||
// execute in REQUIRED transaction | ||
transactionTemplate.execute((TransactionStatus status) -> { | ||
// fetch the message to have it in current hibernate session | ||
final Message loaded = messageDao.findMessage(message.getId()); | ||
|
||
LOG.trace("Message fetched successfully"); | ||
// invoke the processing method. | ||
doProcessMessage(loaded); | ||
return null; // callback without result | ||
}); | ||
|
||
LOG.trace("Message processing finished."); | ||
} | ||
|
||
/** | ||
* Get instance of messageDao repository. | ||
* | ||
* @return the dao bean. | ||
*/ | ||
protected MessageDao getMessageDao() { | ||
return messageDao; | ||
} | ||
|
||
/** | ||
* Do process message. | ||
* | ||
* @param message the message entity. | ||
*/ | ||
protected abstract void doProcessMessage(Message message); | ||
} |
112 changes: 112 additions & 0 deletions
112
...openhubframework/openhub/core/common/asynch/finalmessage/DeleteFinalMessageProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.openhubframework.openhub.core.common.asynch.finalmessage; | ||
|
||
import org.openhubframework.openhub.api.asynch.finalmessage.FinalMessageProcessor; | ||
import org.openhubframework.openhub.api.entity.Message; | ||
import org.openhubframework.openhub.api.entity.Request; | ||
import org.openhubframework.openhub.core.common.dao.ExternalCallDao; | ||
import org.openhubframework.openhub.core.common.dao.RequestResponseDao; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
|
||
/** | ||
* FinalMessageProcessor implementation that does delete given message from datastore. | ||
* | ||
* It does delete message and all related entities as well. | ||
* | ||
* @author Karel Kovarik | ||
* @since 2.1 | ||
* @see FinalMessageProcessor | ||
*/ | ||
@Qualifier(DeleteFinalMessageProcessor.QUALIFIER) | ||
public class DeleteFinalMessageProcessor extends AbstractFinalMessageProcessor { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(DeleteFinalMessageProcessor.class); | ||
|
||
/** | ||
* Qualifier used for the bean. | ||
*/ | ||
public static final String QUALIFIER = "deleteFinalMessageProcessor"; | ||
|
||
/** | ||
* The order used in FinalMessageProcessor hierarchy, see {@link FinalMessageProcessor#getOrder()}. | ||
*/ | ||
public static final int ORDER = -1_000; | ||
|
||
@Autowired | ||
protected RequestResponseDao requestResponseDao; | ||
|
||
@Autowired | ||
protected ExternalCallDao externalCallDao; | ||
|
||
@Override | ||
protected void doProcessMessage(final Message message) { | ||
// delete external calls | ||
deleteExternalCalls(message); | ||
|
||
// delete request responses | ||
deleteRequestsResponses(message); | ||
|
||
// delete message entity itself | ||
deleteMessageEntity(message); | ||
|
||
LOG.debug("Message [{}] deleted.", message.getId()); | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return ORDER; | ||
} | ||
|
||
/** | ||
* Delete all external calls for given Message. | ||
* | ||
* @param message the message to be deleted. | ||
*/ | ||
protected void deleteExternalCalls(final Message message) { | ||
message.getExternalCalls().stream() | ||
.peek(externalCall -> LOG.debug("Will delete external call [{}].", externalCall.getId())) | ||
.forEach(externalCall -> externalCallDao.delete(externalCall)); | ||
} | ||
|
||
/** | ||
* Delete all request & responses for given Message. | ||
* | ||
* @param message the message to be deleted. | ||
*/ | ||
protected void deleteRequestsResponses(final Message message) { | ||
for (Request request : message.getRequests()) { | ||
LOG.debug("Will delete request [{}] and response [{}].", | ||
request.getId(), | ||
request.getResponse() != null ? request.getResponse().getId() : null); | ||
requestResponseDao.deleteResponse(request.getResponse()); | ||
requestResponseDao.deleteRequest(request); | ||
} | ||
} | ||
|
||
/** | ||
* Delete message entity itself. | ||
* | ||
* @param message the message to be deleted. | ||
*/ | ||
protected void deleteMessageEntity(final Message message) { | ||
getMessageDao().delete(message); | ||
} | ||
} |
Oops, something went wrong.