List of usage examples for org.springframework.transaction.support TransactionTemplate execute
@Override @Nullable public <T> T execute(TransactionCallback<T> action) throws TransactionException
From source file:org.ms123.common.workflow.tasks.TaskScriptExecutor.java
@Override public void execute(DelegateExecution execution) { final TaskContext tc = new TaskContext(execution); if (script == null) { return;/* w ww .j av a 2 s. co m*/ } tc.setScript(script.getValue(execution).toString()); if (m_ownTransaction) { TransactionTemplate tt = getTransactionService().getTransactionTemplate(true); tt.execute(new TransactionCallback<Object>() { public Object doInTransaction(TransactionStatus paramTransactionStatus) { _execute(tc, null); return null; } }); } else { _execute(tc, null); } }
From source file:org.ms123.common.workflow.tasks.TaskScriptExecutor.java
public void execute(String namespace, String processDefinitionKey, String pid, String script, final Map addVars, VariableScope variableScope, String hint, DataLayer dataLayer, WorkflowService ws) { if (script == null) { return;/*from ww w. java 2 s .c o m*/ } final TaskContext tc = new TaskContext(); tc.setTenantId(namespace); tc.setProcessDefinitionKey(processDefinitionKey); tc.setHint(hint); tc.setPid(pid); tc.setScript(script); tc.setExecution(variableScope); m_dataLayer = dataLayer; m_workflowService = ws; if (m_ownTransaction) { TransactionTemplate tt = getTransactionService().getTransactionTemplate(true); tt.execute(new TransactionCallback<Object>() { public Object doInTransaction(TransactionStatus paramTransactionStatus) { _execute(tc, addVars); return null; } }); } else { _execute(tc, addVars); } }
From source file:org.ms123.common.workflow.TaskScriptExecutor.java
@Override public void execute(DelegateExecution execution) { final TaskContext tc = new TaskContext(); tc.setExecution(execution);//from w ww.j a va 2 s . c o m setCategory(tc); if (script == null) { return; } tc.setScript(script.getValue(execution).toString()); if (m_ownTransaction) { TransactionTemplate tt = getTransactionService().getTransactionTemplate(true); tt.execute(new TransactionCallback<Object>() { public Object doInTransaction(TransactionStatus paramTransactionStatus) { _execute(tc, null); return null; } }); } else { _execute(tc, null); } }
From source file:org.ms123.common.workflow.TaskScriptExecutor.java
public void execute(String namespace, String processDefinitionKey, String pid, String script, final Map addVars, VariableScope variableScope, String hint, DataLayer dataLayer, WorkflowService ws) { if (script == null) { return;//w w w.jav a 2 s . com } final TaskContext tc = new TaskContext(); tc.setCategory(namespace); tc.setProcessDefinitionKey(processDefinitionKey); tc.setHint(hint); tc.setPid(pid); tc.setScript(script); tc.setExecution(variableScope); m_dataLayer = dataLayer; m_workflowService = ws; if (m_ownTransaction) { TransactionTemplate tt = getTransactionService().getTransactionTemplate(true); tt.execute(new TransactionCallback<Object>() { public Object doInTransaction(TransactionStatus paramTransactionStatus) { _execute(tc, addVars); return null; } }); } else { _execute(tc, addVars); } }
From source file:org.openiam.provision.service.DefaultProvisioningService.java
private ProvisionUserResponse addUser(final ProvisionUser pUser, final IdmAuditLog auditLog) { final List<ProvisionDataContainer> dataList = new LinkedList<ProvisionDataContainer>(); ProvisionUserResponse res = new ProvisionUserResponse(); res.setStatus(ResponseStatus.FAILURE); try {/* w w w. ja v a 2 s. c om*/ final TransactionTemplate transactionTemplate = new TransactionTemplate(platformTransactionManager); transactionTemplate.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED); res = transactionTemplate.execute(new TransactionCallback<ProvisionUserResponse>() { @Override public ProvisionUserResponse doInTransaction(TransactionStatus status) { IdmAuditLog idmAuditLog = new IdmAuditLog(); idmAuditLog.setRequestorUserId(pUser.getRequestorUserId()); idmAuditLog.setRequestorPrincipal(pUser.getRequestorLogin()); idmAuditLog.setAction(AuditAction.CREATE_USER.value()); idmAuditLog.setAuditDescription("Provisioning add user: " + pUser.getId() + " with first/last name: " + pUser.getFirstName() + "/" + pUser.getLastName()); if (auditLog != null) { auditLog.addChild(idmAuditLog); idmAuditLog.addParent(auditLog); idmAuditLog = auditLogService.save(idmAuditLog); } idmAuditLog = auditLogService.save(idmAuditLog); ProvisionUserResponse tmpRes = addModifyUser(pUser, true, dataList, idmAuditLog); idmAuditLog = auditLogService.save(idmAuditLog); return tmpRes; } }); if (res.isSuccess()) { provQueueService.enqueue(dataList); } } catch (Throwable t) { log.error("Can't add user", t); } return res; }
From source file:org.openiam.provision.service.DefaultProvisioningService.java
private ProvisionUserResponse modifyUser(final ProvisionUser pUser, final IdmAuditLog auditLog) { final List<ProvisionDataContainer> dataList = new LinkedList<ProvisionDataContainer>(); TransactionTemplate transactionTemplate = new TransactionTemplate(platformTransactionManager); ProvisionUserResponse res = new ProvisionUserResponse(); res.setStatus(ResponseStatus.SUCCESS); try {/* w w w . j ava 2s .co m*/ res = transactionTemplate.execute(new TransactionCallback<ProvisionUserResponse>() { @Override public ProvisionUserResponse doInTransaction(TransactionStatus status) { IdmAuditLog idmAuditLog = new IdmAuditLog(); idmAuditLog.setRequestorUserId(pUser.getRequestorUserId()); idmAuditLog.setRequestorPrincipal(pUser.getRequestorLogin()); idmAuditLog.setAction(AuditAction.MODIFY_USER.value()); LoginEntity loginEntity = loginManager.getByUserIdManagedSys(pUser.getId(), sysConfiguration.getDefaultManagedSysId()); idmAuditLog.setTargetUser(pUser.getId(), loginEntity.getLogin()); idmAuditLog.setAuditDescription("Provisioning modify user: " + pUser.getId() + " with primary identity: " + loginEntity); if (auditLog != null) { auditLog.addChild(idmAuditLog); idmAuditLog.addParent(auditLog); auditLogService.save(auditLog); } idmAuditLog = auditLogService.save(idmAuditLog); ProvisionUserResponse tmpRes = addModifyUser(pUser, false, dataList, idmAuditLog); idmAuditLog = auditLogService.save(idmAuditLog); return tmpRes; } }); if (res.isSuccess()) { provQueueService.enqueue(dataList); } } catch (Throwable t) { t.printStackTrace(); } return res; }
From source file:org.opennms.netmgt.poller.MonitorTester.java
public static void main(String[] args) { Options options = new Options(); Option oI = new Option("i", "ipaddress", true, "IP Address to test [required]"); oI.setRequired(true);/*w w w . j a va 2 s.c o m*/ options.addOption(oI); Option oS = new Option("s", "service", true, "Service name [required]"); oS.setRequired(true); options.addOption(oS); options.addOption("P", "package", true, "Poller Package"); options.addOption("p", "param", true, "Service parameter ~ key=value"); options.addOption("c", "class", true, "Monitor Class"); CommandLineParser parser = new PosixParser(); CommandLine cmd = null; try { cmd = parser.parse(options, args); } catch (ParseException e) { new HelpFormatter().printHelp(80, CMD_SYNTAX, String.format("ERROR: %s%n", e.getMessage()), options, null); System.exit(1); } initialize(); final String packageName = cmd.getOptionValue('P'); final String monitorClass = cmd.getOptionValue('c'); final String ipAddress = cmd.getOptionValue('i'); final String serviceName = cmd.getOptionValue('s'); Map<String, Object> parameters = new HashMap<String, Object>(); if (cmd.hasOption('p')) { for (String parm : cmd.getOptionValues('p')) { String[] data = parm.split("="); if (data.length == 2 && data[0] != null && data[1] != null) { parameters.put(data[0], data[1]); } } } final InetAddress addr = InetAddressUtils.addr(ipAddress); if (addr == null) { throw new IllegalStateException("Error getting InetAddress object for " + ipAddress); } final IpInterfaceDao dao = BeanUtils.getBean("daoContext", "ipInterfaceDao", IpInterfaceDao.class); final TransactionTemplate tt = BeanUtils.getBean("daoContext", "transactionTemplate", TransactionTemplate.class); MonitoredService monSvc = tt.execute(new TransactionCallback<MonitoredService>() { @Override public MonitoredService doInTransaction(TransactionStatus status) { final List<OnmsIpInterface> ips = dao.findByIpAddress(ipAddress); if (ips == null || ips.size() == 0) { System.err.printf("Error: Can't find the IP address %s on the database\n", ipAddress); return null; } if (ips.size() > 1) { System.out.printf( "Warning: there are several IP interface objects associated with the IP address %s (picking the first one)\n", ipAddress); } OnmsNode n = ips.get(0).getNode(); return new SimpleMonitoredService(addr, n.getId(), n.getLabel(), serviceName); } }); if (monSvc == null) { System.exit(1); } try { PollerConfigFactory.init(); } catch (Exception e) { System.err.printf("Error: Can't initialize poller-configuration.xml. %s%n", e.getMessage()); System.exit(1); } PollerConfig config = PollerConfigFactory.getInstance(); System.out.printf("Checking service %s on IP %s%n", serviceName, ipAddress); org.opennms.netmgt.config.poller.Package pkg = packageName == null ? config.getFirstLocalPackageMatch(ipAddress) : config.getPackage(packageName); if (pkg == null) { System.err.printf("Error: Package %s doesn't exist%n", packageName); System.exit(1); } System.out.printf("Package: %s%n", pkg.getName()); Service svc = config.getServiceInPackage(serviceName, pkg); if (svc == null) { System.err.printf("Error: Service %s not defined on package %s%n", serviceName, packageName); System.exit(1); } ServiceMonitor monitor = null; if (monitorClass == null) { monitor = config.getServiceMonitor(serviceName); if (monitor == null) { System.err.printf("Error: Service %s doesn't have a monitor class defined%n", serviceName); System.exit(1); } } else { try { final Class<? extends ServiceMonitor> mc = Class.forName(monitorClass) .asSubclass(ServiceMonitor.class); monitor = mc.newInstance(); } catch (Exception e) { System.err.printf("Error: Can't instantiate %s because %s%n", monitorClass, e.getMessage()); System.exit(1); } } System.out.printf("Monitor: %s%n", monitor.getClass().getName()); if (config.isPolledLocally(ipAddress, serviceName)) { for (Parameter p : svc.getParameters()) { if (!parameters.containsKey(p.getKey())) { String value = p.getValue(); if (value == null) { try { value = JaxbUtils.marshal(p.getAnyObject()); } catch (Exception e) { } } parameters.put(p.getKey(), value); } } for (Entry<String, Object> e : parameters.entrySet()) { System.out.printf("Parameter %s : %s%n", e.getKey(), e.getValue()); } try { PollStatus status = monitor.poll(monSvc, parameters); System.out.printf("Available ? %s (status %s[%s])%n", status.isAvailable(), status.getStatusName(), status.getStatusCode()); if (status.isAvailable()) { System.out.printf("Response time: %s%n", status.getResponseTime()); } else { if (status.getReason() != null) { System.out.printf("Reason: %s%n", status.getReason()); } } } catch (Exception e) { System.err.println("Error: Can't execute the monitor. " + e.getMessage()); System.exit(1); } } else { System.err.printf("Error: Polling is not enabled for service %s using IP %s%n", serviceName, ipAddress); } System.exit(0); }
From source file:org.opennms.netmgt.scriptd.ins.events.InsSession.java
private void getEventsByCriteria() { LOG.debug("clearing events"); clearEvents();//www. j a v a 2s . co m final BeanFactoryReference bf = BeanUtils.getBeanFactory("daoContext"); final EventDao eventDao = BeanUtils.getBean(bf, "eventDao", EventDao.class); final TransactionTemplate transTemplate = BeanUtils.getBean(bf, "transactionTemplate", TransactionTemplate.class); try { transTemplate.execute(new TransactionCallbackWithoutResult() { @Override public void doInTransactionWithoutResult(final TransactionStatus status) { LOG.debug("Entering transaction call back: selection with criteria: {}", criteriaRestriction); final OnmsCriteria criteria = new OnmsCriteria(OnmsEvent.class); criteria.add(Restrictions.sqlRestriction(criteriaRestriction)); final List<OnmsEvent> events = eventDao.findMatching(criteria); LOG.info("Found {} event(s) with criteria: {}", events.size(), criteriaRestriction); for (final OnmsEvent onmsEvent : events) { final Event xmlEvent = getXMLEvent(onmsEvent); if (xmlEvent != null) addEvent(xmlEvent); } } }); } catch (final RuntimeException e) { LOG.error("Error while getting events.", e); } }
From source file:org.opennms.web.rest.RemotePollerAvailabilityRestServiceTest.java
@Test public void testGetAvailability() { final long endMillis = System.currentTimeMillis(); final long startMillis = endMillis - 12000; final long totalTime = endMillis - startMillis; TransactionTemplate txTemplate = getBean("transactionTemplate", TransactionTemplate.class); txTemplate.execute(new TransactionCallbackWithoutResult() { @Override/* w w w. j a v a 2 s . c o m*/ protected void doInTransactionWithoutResult(TransactionStatus status) { final TimeChunker timeChunker = new TimeChunker(totalTime, new Date(startMillis), new Date(endMillis)); // increment the time segment timeChunker.getNextSegment(); final Collection<OnmsLocationSpecificStatus> allStatusChanges = m_locationMonitorDao .getStatusChangesForApplicationBetween(new Date(startMillis), new Date(endMillis), "IPv6"); final AvailCalculator calc = new AvailCalculator(timeChunker); for (final OnmsLocationSpecificStatus statusChange : allStatusChanges) { calc.onStatusChange(statusChange); } final Collection<OnmsMonitoredService> svcs = m_monServiceDao .findByApplication(m_applicationDao.findByName("IPv6")); final double avail = calc.getAvailabilityFor(svcs, 0); assertEquals(0.8333, avail, 0.0333); } }); }
From source file:org.opennms.web.rest.RemotePollerAvailabilityRestServiceTest.java
private void createLocationMonitors() throws InterruptedException { TransactionTemplate txTemplate = getBean("transactionTemplate", TransactionTemplate.class); txTemplate.execute(new TransactionCallbackWithoutResult() { @Override//from ww w . ja v a2 s .co m protected void doInTransactionWithoutResult(TransactionStatus status) { System.err.println("======= Starting createLocationMonitors() ======"); OnmsLocationMonitor locMon1 = new OnmsLocationMonitor(); locMon1.setDefinitionName("RDU"); locMon1.setLastCheckInTime(new Date()); locMon1.setStatus(MonitorStatus.STARTED); m_locationMonitorDao.save(locMon1); OnmsApplication ipv6App = new OnmsApplication(); ipv6App.setName("IPv6"); m_applicationDao.saveOrUpdate(ipv6App); OnmsApplication ipv4App = new OnmsApplication(); ipv4App.setName("IPv4"); m_applicationDao.saveOrUpdate(ipv4App); OnmsMonitoredService service2 = m_monServiceDao.findByType("HTTP").get(1); service2.addApplication(ipv4App); ipv4App.addMonitoredService(service2); m_monServiceDao.saveOrUpdate(service2); m_applicationDao.saveOrUpdate(ipv4App); List<OnmsMonitoredService> services = m_monServiceDao.findByType("HTTP"); for (OnmsMonitoredService service : services) { service = m_monServiceDao.findByType("HTTP").get(0); service.addApplication(ipv6App); ipv6App.addMonitoredService(service); m_monServiceDao.saveOrUpdate(service); m_applicationDao.saveOrUpdate(ipv6App); OnmsLocationMonitor locMon = m_locationMonitorDao.findAll().get(0); OnmsLocationSpecificStatus statusChange = new OnmsLocationSpecificStatus(); statusChange.setLocationMonitor(locMon); statusChange.setPollResult(PollStatus.available()); statusChange.setMonitoredService(service); m_locationMonitorDao.saveStatusChange(statusChange); } System.err.println("======= End createLocationMonitors() ======"); } }); Thread.sleep(2000L); txTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { List<OnmsMonitoredService> services = m_monServiceDao.findByType("HTTP"); for (OnmsMonitoredService service : services) { OnmsLocationMonitor locMon = m_locationMonitorDao.findAll().get(0); OnmsLocationSpecificStatus statusChange = new OnmsLocationSpecificStatus(); statusChange.setLocationMonitor(locMon); statusChange.setPollResult(PollStatus.unavailable()); statusChange.setMonitoredService(service); m_locationMonitorDao.saveStatusChange(statusChange); } } }); Thread.sleep(2000L); txTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { List<OnmsMonitoredService> services = m_monServiceDao.findByType("HTTP"); for (OnmsMonitoredService service : services) { OnmsLocationMonitor locMon = m_locationMonitorDao.findAll().get(0); OnmsLocationSpecificStatus statusChange = new OnmsLocationSpecificStatus(); statusChange.setLocationMonitor(locMon); statusChange.setPollResult(PollStatus.available()); statusChange.setMonitoredService(service); m_locationMonitorDao.saveStatusChange(statusChange); } } }); }