0
# Enterprise Services
1
2
Additional enterprise APIs including JavaMail, JCA connectors, batch processing, and concurrency utilities.
3
4
## JavaMail
5
6
```java { .api }
7
public abstract class Message implements Part {
8
public abstract void setFrom(Address address) throws MessagingException;
9
public abstract void addFrom(Address[] addresses) throws MessagingException;
10
public abstract Address[] getFrom() throws MessagingException;
11
public abstract void setRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException;
12
public abstract void addRecipients(Message.RecipientType type, Address[] addresses) throws MessagingException;
13
public abstract Address[] getRecipients(Message.RecipientType type) throws MessagingException;
14
public abstract void setSubject(String subject) throws MessagingException;
15
public abstract String getSubject() throws MessagingException;
16
public abstract void setSentDate(Date date) throws MessagingException;
17
public abstract Date getSentDate() throws MessagingException;
18
public abstract void saveChanges() throws MessagingException;
19
}
20
21
public abstract class Transport extends Service {
22
public static void send(Message msg) throws MessagingException;
23
public static void send(Message msg, Address[] addresses) throws MessagingException;
24
public abstract void sendMessage(Message msg, Address[] addresses) throws MessagingException;
25
}
26
```
27
28
## Batch Processing
29
30
```java { .api }
31
public interface JobOperator {
32
long start(String jobXMLName, Properties jobParameters) throws JobStartException, JobSecurityException;
33
long restart(long executionId, Properties restartParameters) throws JobExecutionAlreadyCompleteException, NoSuchJobExecutionException, JobExecutionNotMostRecentException, JobRestartException, JobSecurityException;
34
void stop(long executionId) throws NoSuchJobExecutionException, JobExecutionNotRunningException, JobSecurityException;
35
void abandon(long executionId) throws NoSuchJobExecutionException, JobExecutionIsRunningException, JobSecurityException;
36
JobExecution getJobExecution(long executionId) throws NoSuchJobExecutionException, JobSecurityException;
37
List<JobExecution> getJobExecutions(JobInstance jobInstance) throws NoSuchJobInstanceException, JobSecurityException;
38
JobInstance getJobInstance(long executionId) throws NoSuchJobExecutionException, JobSecurityException;
39
List<JobInstance> getJobInstances(String jobName, int start, int count) throws NoSuchJobException, JobSecurityException;
40
List<String> getJobNames() throws JobSecurityException;
41
Properties getParameters(long executionId) throws NoSuchJobExecutionException, JobSecurityException;
42
List<StepExecution> getStepExecutions(long jobExecutionId) throws NoSuchJobExecutionException, JobSecurityException;
43
}
44
45
public interface StepContext {
46
String getStepName();
47
Object getTransientUserData();
48
void setTransientUserData(Object data);
49
long getStepExecutionId();
50
Properties getProperties();
51
Serializable getPersistentUserData();
52
void setPersistentUserData(Serializable data);
53
BatchStatus getBatchStatus();
54
String getExitStatus();
55
void setExitStatus(String status);
56
Exception getException();
57
Metric[] getMetrics();
58
}
59
```
60
61
## Concurrency Utilities
62
63
### Managed Executor Services
64
65
```java { .api }
66
public interface ManagedExecutorService extends ExecutorService {
67
<T> Future<T> submit(Callable<T> task);
68
<T> Future<T> submit(Runnable task, T result);
69
Future<?> submit(Runnable task);
70
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException;
71
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException;
72
<T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException;
73
<T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
74
void execute(Runnable command);
75
}
76
77
public interface ManagedScheduledExecutorService extends ManagedExecutorService, ScheduledExecutorService {
78
ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);
79
<V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit);
80
ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);
81
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);
82
}
83
```
84
85
### Managed Thread Factory
86
87
```java { .api }
88
public interface ManagedThreadFactory extends ThreadFactory {
89
Thread newThread(Runnable r);
90
}
91
```
92
93
### Context Service
94
95
```java { .api }
96
public interface ContextService {
97
<T> T createContextualProxy(T instance, Class<T> intf);
98
<T> T createContextualProxy(T instance, Class<?>... interfaces);
99
Map<String, String> getExecutionProperties(Object contextualProxy);
100
<T> Callable<T> contextualizeCallable(Callable<T> callable);
101
Runnable contextualizeRunnable(Runnable runnable);
102
}
103
```
104
105
### Last Execution and Trigger
106
107
```java { .api }
108
public interface LastExecution {
109
String getIdentityName();
110
Date getRunStart();
111
Date getRunEnd();
112
Object getResult();
113
}
114
115
public interface Trigger {
116
Date getNextRunTime(LastExecution lastExecutionInfo, Date taskScheduledTime);
117
boolean skipRun(LastExecution lastExecutionInfo, Date scheduledRunTime);
118
}
119
```
120
121
### Managed Task
122
123
```java { .api }
124
public interface ManagedTask {
125
String IDENTITY_NAME = "javax.enterprise.concurrent.IDENTITY_NAME";
126
String USE_TRANSACTION_OF_EXECUTION_THREAD = "javax.enterprise.concurrent.USE_TRANSACTION_OF_EXECUTION_THREAD";
127
String LONGRUNNING_HINT = "javax.enterprise.concurrent.LONGRUNNING_HINT";
128
String TRANSACTION = "javax.enterprise.concurrent.TRANSACTION";
129
130
Map<String, String> getExecutionProperties();
131
ManagedTaskListener getManagedTaskListener();
132
}
133
134
public interface ManagedTaskListener {
135
void taskSubmitted(Future<?> future, ManagedExecutorService executor, Object task);
136
void taskAborted(Future<?> future, ManagedExecutorService executor, Object task, Throwable exception);
137
void taskDone(Future<?> future, ManagedExecutorService executor, Object task, Throwable exception);
138
void taskStarting(Future<?> future, ManagedExecutorService executor, Object task);
139
}
140
```
141
142
## Usage Examples
143
144
```java
145
// JavaMail
146
@Resource(name = "mail/MyMailSession")
147
Session mailSession;
148
149
public void sendEmail(String to, String subject, String body) throws MessagingException {
150
Message message = new MimeMessage(mailSession);
151
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
152
message.setSubject(subject);
153
message.setText(body);
154
Transport.send(message);
155
}
156
157
// Batch Processing
158
@Named
159
public class MyItemProcessor implements ItemProcessor {
160
@Override
161
public Object processItem(Object item) throws Exception {
162
// Process item
163
return processedItem;
164
}
165
}
166
167
// Concurrency
168
@Resource
169
ManagedExecutorService executor;
170
171
public void executeAsync() {
172
executor.submit(() -> {
173
// Asynchronous task
174
});
175
}
176
```