Java Reference
In-Depth Information
The sampling interval for the notifier is taken by the
ContextListener
class
from the application context.
public final class
ContextListener
implements
ServletContextListener {
public
void contextInitialized(ServletContextEvent event) {
...
Long interval
#
(Long)initial.lookup
("java:comp/env/interval");
Manager mgr
#
new Manager(ds,interval.longValue());
...
}
}
The value of the interval attribute of the application context is set in the
application descriptor contained in the
web.xml
file.
<web-app>
...
<env-entry>
<env-entry-name>interval</env-entry-name>
<env-entry-value>10000</env-entry-value>
<env-entry-type>java.lang.Long</env-entry-type>
</env-entry>
</web-app>
The notification is based on the
SMSSender
class. It has a constructor that
tries to find a serial port connected to a GSM modem. To send a message we
need first to open the connection with the
open()
method, then to send one
or more messages using the
send()
method, and finally close the connection
with the
close()
method.
The communication with the modem is carried out by the method
sendCmdNoCR()
. This method writes to the serial port and then blocks for a
given delay. In the meantime the modem sends a reply; the presence of data
coming from the modem is notified asynchronously via the
serialEvent()
method, which reads the available data and puts it into the buffer
answer
.
When the
serialEvent()
method recognizes the end of a reply or the timeout
has expired the
sendCmdNoCR()
method is woken up and returns the content
of the
answer
buffer.
package
sms;
import
java.io.*;
import
java.util.*;
import
javax.comm.*;
// SMSSender is a class that can be used to send SMS messages
// through a mobile phone connected to a serial port.
public class
SMSSender
implements
SerialPortEventListener {
InputStream inputStream;
OutputStream outputStream;
SerialPort serialPort;