001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.util; 018 019 import java.util.ArrayList; 020 import java.util.Collections; 021 import java.util.List; 022 023 import org.apache.camel.Exchange; 024 import org.apache.camel.spi.Synchronization; 025 import org.apache.camel.spi.UnitOfWork; 026 import org.slf4j.Logger; 027 import org.slf4j.LoggerFactory; 028 029 /** 030 * @version 031 */ 032 public final class UnitOfWorkHelper { 033 034 private static final Logger LOG = LoggerFactory.getLogger(UnitOfWorkHelper.class); 035 036 private UnitOfWorkHelper() { 037 } 038 039 /** 040 * Creates a new {@link UnitOfWork}. 041 * 042 * @param exchange the exchange 043 * @return the created unit of work (is not started) 044 * @deprecated use {@link org.apache.camel.CamelContext#getUnitOfWorkFactory()} instead. 045 */ 046 @Deprecated 047 public static UnitOfWork createUoW(Exchange exchange) { 048 return exchange.getContext().getUnitOfWorkFactory().createUnitOfWork(exchange); 049 } 050 051 /** 052 * Done and stop the {@link UnitOfWork}. 053 * 054 * @param uow the unit of work 055 * @param exchange the exchange (will unset the UoW on the exchange) 056 */ 057 public static void doneUow(UnitOfWork uow, Exchange exchange) { 058 // unit of work is done 059 try { 060 if (uow != null) { 061 uow.done(exchange); 062 } 063 } catch (Throwable e) { 064 LOG.warn("Exception occurred during done UnitOfWork for Exchange: " + exchange 065 + ". This exception will be ignored.", e); 066 } 067 try { 068 if (uow != null) { 069 uow.stop(); 070 } 071 } catch (Throwable e) { 072 LOG.warn("Exception occurred during stopping UnitOfWork for Exchange: " + exchange 073 + ". This exception will be ignored.", e); 074 } 075 076 // remove uow from exchange as its done 077 exchange.setUnitOfWork(null); 078 } 079 080 public static void doneSynchronizations(Exchange exchange, List<Synchronization> synchronizations, Logger log) { 081 boolean failed = exchange.isFailed(); 082 083 if (synchronizations != null && !synchronizations.isEmpty()) { 084 // work on a copy of the list to avoid any modification which may cause ConcurrentModificationException 085 List<Synchronization> copy = new ArrayList<Synchronization>(synchronizations); 086 087 // reverse so we invoke it FILO style instead of FIFO 088 Collections.reverse(copy); 089 // and honor if any was ordered by sorting it accordingly 090 Collections.sort(copy, new OrderedComparator()); 091 092 // invoke synchronization callbacks 093 for (Synchronization synchronization : copy) { 094 try { 095 if (failed) { 096 log.trace("Invoking synchronization.onFailure: {} with {}", synchronization, exchange); 097 synchronization.onFailure(exchange); 098 } else { 099 log.trace("Invoking synchronization.onComplete: {} with {}", synchronization, exchange); 100 synchronization.onComplete(exchange); 101 } 102 } catch (Throwable e) { 103 // must catch exceptions to ensure all synchronizations have a chance to run 104 log.warn("Exception occurred during onCompletion. This exception will be ignored.", e); 105 } 106 } 107 } 108 } 109 110 }