use of org.apache.openejb.jee.TimerSchedule in project tomee by apache.
the class Timer$JAXB method _read.
public static final Timer _read(final XoXMLStreamReader reader, RuntimeContext context) throws Exception {
// Check for xsi:nil
if (reader.isXsiNil()) {
return null;
}
if (context == null) {
context = new RuntimeContext();
}
final Timer timer = new Timer();
context.beforeUnmarshal(timer, LifecycleCallback.NONE);
ArrayList<Text> descriptions = null;
// Check xsi:type
final QName xsiType = reader.getXsiType();
if (xsiType != null) {
if (("timerType" != xsiType.getLocalPart()) || ("http://java.sun.com/xml/ns/javaee" != xsiType.getNamespaceURI())) {
return context.unexpectedXsiType(reader, Timer.class);
}
}
// Read attributes
for (final Attribute attribute : reader.getAttributes()) {
if (("id" == attribute.getLocalName()) && (("" == attribute.getNamespace()) || (attribute.getNamespace() == null))) {
// ATTRIBUTE: id
final String id = Adapters.collapsedStringAdapterAdapter.unmarshal(attribute.getValue());
context.addXmlId(reader, id, timer);
timer.id = id;
} else if (XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI != attribute.getNamespace()) {
context.unexpectedAttribute(attribute, new QName("", "id"));
}
}
// Read elements
for (final XoXMLStreamReader elementReader : reader.getChildElements()) {
if (("description" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
// ELEMENT: descriptions
final Text descriptionsItem = readText(elementReader, context);
if (descriptions == null) {
descriptions = new ArrayList<Text>();
}
descriptions.add(descriptionsItem);
} else if (("schedule" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
// ELEMENT: schedule
final TimerSchedule schedule = readTimerSchedule(elementReader, context);
timer.schedule = schedule;
} else if (("start" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
// ELEMENT: start
final XMLGregorianCalendar start = datatypeFactory.newXMLGregorianCalendar(elementReader.getElementAsString());
timer.start = start;
} else if (("end" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
// ELEMENT: end
final XMLGregorianCalendar end = datatypeFactory.newXMLGregorianCalendar(elementReader.getElementAsString());
timer.end = end;
} else if (("timeout-method" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
// ELEMENT: timeoutMethod
final NamedMethod timeoutMethod = readNamedMethod(elementReader, context);
timer.timeoutMethod = timeoutMethod;
} else if (("persistent" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
// ELEMENT: persistent
final Boolean persistent = ("1".equals(elementReader.getElementAsString()) || "true".equals(elementReader.getElementAsString()));
timer.persistent = persistent;
} else if (("timezone" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
// ELEMENT: timezone
final String timezoneRaw = elementReader.getElementAsString();
final String timezone;
try {
timezone = Adapters.collapsedStringAdapterAdapter.unmarshal(timezoneRaw);
} catch (final Exception e) {
context.xmlAdapterError(elementReader, CollapsedStringAdapter.class, String.class, String.class, e);
continue;
}
timer.timezone = timezone;
} else if (("info" == elementReader.getLocalName()) && ("http://java.sun.com/xml/ns/javaee" == elementReader.getNamespaceURI())) {
// ELEMENT: info
final String infoRaw = elementReader.getElementAsString();
final String info;
try {
info = Adapters.collapsedStringAdapterAdapter.unmarshal(infoRaw);
} catch (final Exception e) {
context.xmlAdapterError(elementReader, CollapsedStringAdapter.class, String.class, String.class, e);
continue;
}
timer.info = info;
} else {
context.unexpectedElement(elementReader, new QName("http://java.sun.com/xml/ns/javaee", "description"), new QName("http://java.sun.com/xml/ns/javaee", "schedule"), new QName("http://java.sun.com/xml/ns/javaee", "start"), new QName("http://java.sun.com/xml/ns/javaee", "end"), new QName("http://java.sun.com/xml/ns/javaee", "timeout-method"), new QName("http://java.sun.com/xml/ns/javaee", "persistent"), new QName("http://java.sun.com/xml/ns/javaee", "timezone"), new QName("http://java.sun.com/xml/ns/javaee", "info"));
}
}
if (descriptions != null) {
try {
timer.setDescriptions(descriptions.toArray(new Text[descriptions.size()]));
} catch (final Exception e) {
context.setterError(reader, Timer.class, "setDescriptions", Text[].class, e);
}
}
context.afterUnmarshal(timer, LifecycleCallback.NONE);
return timer;
}
use of org.apache.openejb.jee.TimerSchedule in project tomee by apache.
the class ScheduleTest method testSchedule.
public void testSchedule() throws Exception {
System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
final Assembler assembler = new Assembler();
final ConfigurationFactory config = new ConfigurationFactory();
assembler.createProxyFactory(config.configureService(ProxyFactoryInfo.class));
assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
final EjbJar ejbJar = new EjbJar();
// Configure schedule by deployment plan
final StatelessBean subBeanA = new StatelessBean(SubBeanA.class);
final Timer subBeanATimer = new Timer();
subBeanATimer.setTimeoutMethod(new NamedMethod("subBeanA", "javax.ejb.Timer"));
final TimerSchedule timerScheduleA = new TimerSchedule();
timerScheduleA.setSecond("2");
timerScheduleA.setMinute("*");
timerScheduleA.setHour("*");
subBeanATimer.setSchedule(timerScheduleA);
subBeanATimer.setInfo("SubBeanAInfo");
subBeanA.getTimer().add(subBeanATimer);
ejbJar.addEnterpriseBean(subBeanA);
// Configure schedule by annotation
final StatelessBean subBeanB = new StatelessBean(SubBeanB.class);
ejbJar.addEnterpriseBean(subBeanB);
// Override aroundTimeout annotation by deployment plan
final StatelessBean subBeanC = new StatelessBean(SubBeanC.class);
final Timer subBeanCTimer = new Timer();
subBeanCTimer.setTimeoutMethod(new NamedMethod("subBeanC", "javax.ejb.Timer"));
final TimerSchedule timerScheduleC = new TimerSchedule();
timerScheduleC.setSecond("2");
timerScheduleC.setMinute("*");
timerScheduleC.setHour("*");
subBeanCTimer.setSchedule(timerScheduleC);
subBeanCTimer.setInfo("SubBeanCInfo");
subBeanC.getTimer().add(subBeanCTimer);
ejbJar.addEnterpriseBean(subBeanC);
final StatefulBean subBeanM = new StatefulBean(SubBeanM.class);
ejbJar.addEnterpriseBean(subBeanM);
final EjbJarInfo ejbJarInfo = config.configureApplication(ejbJar);
assembler.createApplication(ejbJarInfo);
countDownLatch.await(1L, TimeUnit.MINUTES);
// A better way for validation ?
int beforeAroundInvocationCount = 0;
int afterAroundInvocationCount = 0;
int timeoutInvocationCount = 0;
final int size;
synchronized (result) {
size = result.size();
for (final Call call : result) {
switch(call) {
case BEAN_BEFORE_AROUNDTIMEOUT:
beforeAroundInvocationCount++;
break;
case BEAN_AFTER_AROUNDTIMEOUT:
afterAroundInvocationCount++;
break;
case TIMEOUT:
timeoutInvocationCount++;
break;
}
}
}
assertEquals(3, beforeAroundInvocationCount);
assertEquals(3, afterAroundInvocationCount);
assertEquals(3, timeoutInvocationCount);
assertEquals(9, size);
}
use of org.apache.openejb.jee.TimerSchedule in project tomee by apache.
the class EjbJarInfoBuilder method copySchedules.
private void copySchedules(final List<Timer> timers, final List<MethodScheduleInfo> scheduleInfos) {
final Map<NamedMethod, MethodScheduleInfo> methodScheduleInfoMap = new HashMap<>();
for (final Timer timer : timers) {
final NamedMethod timeoutMethod = timer.getTimeoutMethod();
MethodScheduleInfo methodScheduleInfo = methodScheduleInfoMap.get(timer.getTimeoutMethod());
if (methodScheduleInfo == null) {
methodScheduleInfo = new MethodScheduleInfo();
methodScheduleInfoMap.put(timeoutMethod, methodScheduleInfo);
methodScheduleInfo.method = toInfo(timeoutMethod);
}
final ScheduleInfo scheduleInfo = new ScheduleInfo();
// Copy TimerSchedule
final TimerSchedule timerSchedule = timer.getSchedule();
if (timerSchedule != null) {
scheduleInfo.second = timerSchedule.getSecond();
scheduleInfo.minute = timerSchedule.getMinute();
scheduleInfo.hour = timerSchedule.getHour();
scheduleInfo.dayOfWeek = timerSchedule.getDayOfWeek();
scheduleInfo.dayOfMonth = timerSchedule.getDayOfMonth();
scheduleInfo.month = timerSchedule.getMonth();
scheduleInfo.year = timerSchedule.getYear();
}
// Copy other attributes
scheduleInfo.timezone = timer.getTimezone();
if (timer.getStart() != null) {
scheduleInfo.start = timer.getStart().toGregorianCalendar().getTime();
}
if (timer.getEnd() != null) {
scheduleInfo.end = timer.getEnd().toGregorianCalendar().getTime();
}
scheduleInfo.info = timer.getInfo();
if (timer.getPersistent() != null) {
scheduleInfo.persistent = timer.getPersistent();
}
methodScheduleInfo.schedules.add(scheduleInfo);
}
scheduleInfos.addAll(methodScheduleInfoMap.values());
}
use of org.apache.openejb.jee.TimerSchedule in project tomee by apache.
the class Timer$JAXB method _write.
public static final void _write(final XoXMLStreamWriter writer, final Timer timer, RuntimeContext context) throws Exception {
if (timer == null) {
writer.writeXsiNil();
return;
}
if (context == null) {
context = new RuntimeContext();
}
final String prefix = writer.getUniquePrefix("http://java.sun.com/xml/ns/javaee");
if (Timer.class != timer.getClass()) {
context.unexpectedSubclass(writer, timer, Timer.class);
return;
}
context.beforeMarshal(timer, LifecycleCallback.NONE);
// ATTRIBUTE: id
final String idRaw = timer.id;
if (idRaw != null) {
String id = null;
try {
id = Adapters.collapsedStringAdapterAdapter.marshal(idRaw);
} catch (final Exception e) {
context.xmlAdapterError(timer, "id", CollapsedStringAdapter.class, String.class, String.class, e);
}
writer.writeAttribute("", "", "id", id);
}
// ELEMENT: descriptions
Text[] descriptions = null;
try {
descriptions = timer.getDescriptions();
} catch (final Exception e) {
context.getterError(timer, "descriptions", Timer.class, "getDescriptions", e);
}
if (descriptions != null) {
for (final Text descriptionsItem : descriptions) {
if (descriptionsItem != null) {
writer.writeStartElement(prefix, "description", "http://java.sun.com/xml/ns/javaee");
writeText(writer, descriptionsItem, context);
writer.writeEndElement();
} else {
context.unexpectedNullValue(timer, "descriptions");
}
}
}
// ELEMENT: schedule
final TimerSchedule schedule = timer.schedule;
if (schedule != null) {
writer.writeStartElement(prefix, "schedule", "http://java.sun.com/xml/ns/javaee");
writeTimerSchedule(writer, schedule, context);
writer.writeEndElement();
} else {
context.unexpectedNullValue(timer, "schedule");
}
// ELEMENT: start
final XMLGregorianCalendar start = timer.start;
if (start != null) {
writer.writeStartElement(prefix, "start", "http://java.sun.com/xml/ns/javaee");
writer.writeCharacters(start.toXMLFormat());
writer.writeEndElement();
}
// ELEMENT: end
final XMLGregorianCalendar end = timer.end;
if (end != null) {
writer.writeStartElement(prefix, "end", "http://java.sun.com/xml/ns/javaee");
writer.writeCharacters(end.toXMLFormat());
writer.writeEndElement();
}
// ELEMENT: timeoutMethod
final NamedMethod timeoutMethod = timer.timeoutMethod;
if (timeoutMethod != null) {
writer.writeStartElement(prefix, "timeout-method", "http://java.sun.com/xml/ns/javaee");
writeNamedMethod(writer, timeoutMethod, context);
writer.writeEndElement();
} else {
context.unexpectedNullValue(timer, "timeoutMethod");
}
// ELEMENT: persistent
final Boolean persistent = timer.persistent;
if (persistent != null) {
writer.writeStartElement(prefix, "persistent", "http://java.sun.com/xml/ns/javaee");
writer.writeCharacters(Boolean.toString(persistent));
writer.writeEndElement();
}
// ELEMENT: timezone
final String timezoneRaw = timer.timezone;
String timezone = null;
try {
timezone = Adapters.collapsedStringAdapterAdapter.marshal(timezoneRaw);
} catch (final Exception e) {
context.xmlAdapterError(timer, "timezone", CollapsedStringAdapter.class, String.class, String.class, e);
}
if (timezone != null) {
writer.writeStartElement(prefix, "timezone", "http://java.sun.com/xml/ns/javaee");
writer.writeCharacters(timezone);
writer.writeEndElement();
}
// ELEMENT: info
final String infoRaw = timer.info;
String info = null;
try {
info = Adapters.collapsedStringAdapterAdapter.marshal(infoRaw);
} catch (final Exception e) {
context.xmlAdapterError(timer, "info", CollapsedStringAdapter.class, String.class, String.class, e);
}
if (info != null) {
writer.writeStartElement(prefix, "info", "http://java.sun.com/xml/ns/javaee");
writer.writeCharacters(info);
writer.writeEndElement();
}
context.afterMarshal(timer, LifecycleCallback.NONE);
}
Aggregations