I am trying to write a multi-threaded server program with 2 servers.I am testing with 2 clients.Sometimes depending on a md5 hashcode a client connected to a server has to disconnect from said server and connect to the other one.Sometimes this happens without any problems and sometimes I get java.net.SocketException: Socket closed.Here s the code
Broker class(Server):
import java.io.IOException;
import java.math.BigInteger;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.*;
import java.util.ArrayList;
import java.util.List;
public class Broker extends Node implements Runnable {
private static List<Publisher> registeredpublishers = new ArrayList<Publisher>();
private static List<Consumer> registeredConsumers = new ArrayList<Consumer>();
public static List<Consumer> GetConsumers(){
return registeredConsumers;
}
public String Name;
public Integer port;
public Broker(Integer port,String name){
this.port=port;
this.Name=name;
}
ServerSocket providerSocket;
Socket connection = null;
String ip="127.0.0.1";
BigInteger myKeys;
public void run(){
calculateKeys();
Node.getBrokers().add(this);
openServer();
}
void calculateKeys(){
String g =ip+ (port != null ? port.toString() : null);
MessageDigest m = null;
try {
m = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
m.reset();
m.update(g.getBytes());
byte[] digest = m.digest();
myKeys = new BigInteger(1,digest);
BigInteger a=new BigInteger("25");
myKeys=myKeys.mod(a);
System.out.println(myKeys);
}
void openServer()throws NullPointerException {
try {
providerSocket = new ServerSocket(this.port, 10);
while (true) {
acceptConnection();
new BrokerHandler(connection,this).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
providerSocket.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
void acceptConnection()throws NullPointerException {
try {
connection = providerSocket.accept();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("client connected.");
{
}
}
public static void main(String args[]) {
new Thread(new Broker(54319,"First")).start();
new Thread(new Broker(12320,"Second")).start();
}
}
BrokerHandler Class
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.math.BigInteger;
import java.net.Socket;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class BrokerHandler extends Thread implements Serializable {
ObjectInputStream in;
ObjectOutputStream out;
String f;
BigInteger theirKeys;
Broker broker;
Object e;
Message request;
public BrokerHandler(Socket connection,Broker broker) throws NullPointerException{
try {
in = new ObjectInputStream(connection.getInputStream());
out =new ObjectOutputStream(connection.getOutputStream());
try {
this.request=(Message)in.readObject();
this.f=request.artist;
this.e =request.entity;
this.broker=broker;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
if(this.e instanceof Consumer){
calculateMessageKeys(this.request);
checkBroker(this.broker,(Consumer) e);
}
}
public synchronized void disconnect(Socket connection){
try {
in.close();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}finally {
try {
connection.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public void checkBroker(Broker broker,Consumer consumer) {
int intMyKeys = broker.myKeys.intValue();
int intTheirKeys = theirKeys.intValue();
if (intTheirKeys > 23) {
intTheirKeys = intTheirKeys % 23;
}
if (intTheirKeys <= intMyKeys && intTheirKeys >= intMyKeys - 11) {
consumer.Register(broker, f);
System.out.println(broker.Name + "Client Connected and Registered");
} else {
int thePort = 0;
System.out.println(broker.Name + "Client changing server");
for (Broker broker1 : Node.getBrokers()) {
int KEYS = broker1.myKeys.intValue();
if (intTheirKeys <= KEYS && intTheirKeys >= KEYS - 11) {
thePort = broker1.port;
System.out.println(thePort);
}
}
disconnect(broker.connection);
Consumer a = new Consumer(consumer.artist, thePort);
new ConsumerHandler(a).start();
}
}
public void calculateMessageKeys(Message request) {
MessageDigest m = null;
try {
m = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
m.reset();
m.update(f.getBytes());
byte[] digest = m.digest();
theirKeys = new BigInteger(1,digest);
BigInteger mod=new BigInteger("25");
theirKeys=theirKeys.mod(mod);
System.out.println(theirKeys);
}
}
Consumer class
import java.io.*;
import java.util.Random;
public class Consumer extends Node implements Serializable {
String artist;
Random r=new Random();
int max=12320;
int min=54319;
int port;
Message request;
public Consumer(String artist){
this.artist=artist;
this.port=new Random().nextBoolean() ? max : min;
request= new Message(artist,this.getConsumer());
}
public Consumer(String artist,int port){
this.artist=artist;
this.port=port;
request= new Message(artist,this.getConsumer());
}
public Consumer getConsumer(){
return this;
}
public void Register(Broker broker,String ArtistName){
broker.GetConsumers().add(this);
System.out.println(this.artist);
}
}
ConsumerHandle Class
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class ConsumerHandler extends Thread{
Consumer consumer;
public ConsumerHandler(Consumer consumer){
this.consumer=consumer;
}
void connect(int port) {
Socket requestSocket=null;
ObjectOutputStream out = null;
ObjectInputStream in = null;
try {
requestSocket = new Socket("127.0.0.1", consumer.port);
out = new ObjectOutputStream(requestSocket.getOutputStream());
in = new ObjectInputStream(requestSocket.getInputStream());
System.out.println("Message created.");
out.writeObject(consumer.request);
} catch (UnknownHostException unknownHost) {
System.err.println("You are trying to connect to an unknown host!");
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
try {
in.close();
out.close();
requestSocket.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
public void run(){
connect(consumer.port);
}
public static void main(String args[]) {
Consumer a=new Consumer("Kevin MacLeod");
Consumer b=new Consumer("Alexander Narakada");
new ConsumerHandler(a).start();
new ConsumerHandler(b).start();
}
}
and the stack trace of when it happens:
java.net.SocketException: Socket closed
at java.base/sun.nio.ch.NioSocketImpl.ensureOpenAndConnected(NioSocketImpl.java:166)
at java.base/sun.nio.ch.NioSocketImpl.beginRead(NioSocketImpl.java:232)
at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:300)
at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:351)
at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:802)
at java.base/java.net.Socket$SocketInputStream.read(Socket.java:937)
at java.base/java.net.Socket$SocketInputStream.read(Socket.java:932)
at java.base/java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java:2778)
at java.base/java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream.java:3105)
at java.base/java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:3115)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1597)
at java.base/java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2410)
at java.base/java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2304)
at java.base/java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2142)
at java.base/java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1646)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:464)
at java.base/java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
at BrokerHandler.<init>(BrokerHandler.java:26)
at Broker.openServer(Broker.java:59)
at Broker.run(Broker.java:34)
at java.base/java.lang.Thread.run(Thread.java:830)
I'm making an application with Google SpeechClient that has the requirements to set a GOOGLE_APPLICATION_CREDENTIALS environment variable that, once set, you can use the voice to text api.
My application is required to run in linux and windows. In linux it runs perfectly, however, on windows, when running the project, it throws an exception com.google.api.gax.rpc.UnavailableException: "io.grpc.StatusRuntimeException: UNAVAILABLE: Credentials failed to obtain metadata" when trying to run this thread
package Controller.Runnables;
import Controller.GUI.VoxSpeechGUIController;
import Model.SpokenTextHistory;
import com.google.api.gax.rpc.ClientStream;
import com.google.api.gax.rpc.ResponseObserver;
import com.google.api.gax.rpc.StreamController;
import com.google.cloud.speech.v1.*;
import com.google.protobuf.ByteString;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
import java.io.IOException;
import java.util.ArrayList;
public class SpeechRecognizerRunnable implements Runnable{
private VoxSpeechGUIController controller;
public SpeechRecognizerRunnable(VoxSpeechGUIController voxSpeechGUIController) {
this.controller = voxSpeechGUIController;
}
#Override
public void run() {
MicrofoneRunnable micrunnable = MicrofoneRunnable.getInstance();
Thread micThread = new Thread(micrunnable);
ResponseObserver<StreamingRecognizeResponse> responseObserver = null;
try (SpeechClient client = SpeechClient.create()) {
ClientStream<StreamingRecognizeRequest> clientStream;
responseObserver =
new ResponseObserver<StreamingRecognizeResponse>() {
ArrayList<StreamingRecognizeResponse> responses = new ArrayList<>();
public void onStart(StreamController controller) {}
public void onResponse(StreamingRecognizeResponse response) {
try {
responses.add(response);
StreamingRecognitionResult result = response.getResultsList().get(0);
// There can be several alternative transcripts for a given chunk of speech. Just
// use the first (most likely) one here.
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
String transcript = alternative.getTranscript();
System.out.printf("Transcript : %s\n", transcript);
String newText = SpokenTextHistory.getInstance().getActualSpeechString() + " " + transcript;
SpokenTextHistory.getInstance().setActualSpeechString(newText);
controller.setLabelText(newText);
}
catch (Exception ex){
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
public void onComplete() {
}
public void onError(Throwable t) {
System.out.println(t);
}
};
clientStream = client.streamingRecognizeCallable().splitCall(responseObserver);
RecognitionConfig recognitionConfig =
RecognitionConfig.newBuilder()
.setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
.setLanguageCode("pt-BR")
.setSampleRateHertz(16000)
.build();
StreamingRecognitionConfig streamingRecognitionConfig =
StreamingRecognitionConfig.newBuilder().setConfig(recognitionConfig).build();
StreamingRecognizeRequest request =
StreamingRecognizeRequest.newBuilder()
.setStreamingConfig(streamingRecognitionConfig)
.build(); // The first request in a streaming call has to be a config
clientStream.send(request);
try {
// SampleRate:16000Hz, SampleSizeInBits: 16, Number of channels: 1, Signed: true,
// bigEndian: false
AudioFormat audioFormat = new AudioFormat(16000, 16, 1, true, false);
DataLine.Info targetInfo =
new DataLine.Info(
TargetDataLine.class,
audioFormat); // Set the system information to read from the microphone audio
// stream
if (!AudioSystem.isLineSupported(targetInfo)) {
System.out.println("Microphone not supported");
System.exit(0);
}
// Target data line captures the audio stream the microphone produces.
micrunnable.targetDataLine = (TargetDataLine) AudioSystem.getLine(targetInfo);
micrunnable.targetDataLine.open(audioFormat);
micThread.start();
long startTime = System.currentTimeMillis();
while (!micrunnable.stopFlag) {
long estimatedTime = System.currentTimeMillis() - startTime;
if (estimatedTime >= 55000) {
clientStream.closeSend();
clientStream = client.streamingRecognizeCallable().splitCall(responseObserver);
request =
StreamingRecognizeRequest.newBuilder()
.setStreamingConfig(streamingRecognitionConfig)
.build();
startTime = System.currentTimeMillis();
} else {
request =
StreamingRecognizeRequest.newBuilder()
.setAudioContent(ByteString.copyFrom(micrunnable.sharedQueue.take()))
.build();
}
clientStream.send(request);
}
} catch (Exception e) {
System.out.println(e);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
I've been working hard for hours and have not found a solution that solves my problem.
It is worth mentioning that the environment variable is being set correctly.
Has anyone ever had this problem with Google? What should I do to fix this?
This is my envirounment variable creator:
PS: I`ve already tried use all google alternatives to validate credentials, but all return me errors.
package Controller.Autentication;
import java.io.*;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class GoogleAuthentication {
private static final String GOOGLE_APPLICATION_CREDENTIALS = "GOOGLE_APPLICATION_CREDENTIALS";
private static final String VoxSpeechFolder = ".vox";
private static final String GoogleAuthenticationJsonFile = "VoxAuthentication.json";
public static void setupGoogleCredentials() {
String directory = defaultDirectory();
directory += File.separator+VoxSpeechFolder;
File voxPath = new File(directory);
if (!voxPath.exists()) {
voxPath.mkdirs();
}
ClassLoader classLoader = new GoogleAuthentication().getClass().getClassLoader();
File srcFile = new File(classLoader.getResource(GoogleAuthenticationJsonFile).getFile());
if(srcFile.exists()){
try {
String voxDestPath = defaultDirectory() + File.separator + VoxSpeechFolder +File.separator+ GoogleAuthenticationJsonFile;
File destFile = new File(voxDestPath);
copyFile(srcFile,destFile);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Map<String,String> googleEnv = new HashMap<>();
String path = defaultDirectory() +File.separator+ VoxSpeechFolder +File.separator+ GoogleAuthenticationJsonFile;
googleEnv.put(GOOGLE_APPLICATION_CREDENTIALS, path);
setGoogleEnv(googleEnv);
} catch (Exception e) {
e.printStackTrace();
}
}
static void copyFile(File sourceFile, File destFile)
throws IOException {
InputStream inStream ;
OutputStream outStream ;
System.out.println(destFile.getPath());
if(destFile.createNewFile()){
inStream = new FileInputStream(sourceFile);
outStream = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
}
}
static String defaultDirectory()
{
String OS = getOperationSystem();
if (OS.contains("WIN"))
return System.getenv("APPDATA");
else if (OS.contains("MAC"))
return System.getProperty("user.home") + "/Library/Application "
+ "Support";
else if (OS.contains("LINUX")) {
return System.getProperty("user.home");
}
return System.getProperty("user.dir");
}
static String getOperationSystem() {
return System.getProperty("os.name").toUpperCase();
}
protected static void setGoogleEnv(Map<String, String> newenv) throws Exception {
try {
Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
theEnvironmentField.setAccessible(true);
Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
env.putAll(newenv);
Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
theCaseInsensitiveEnvironmentField.setAccessible(true);
Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
cienv.putAll(newenv);
} catch (NoSuchFieldException e) {
Class[] classes = Collections.class.getDeclaredClasses();
Map<String, String> env = System.getenv();
for(Class cl : classes) {
if("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
Field field = cl.getDeclaredField("m");
field.setAccessible(true);
Object obj = field.get(env);
Map<String, String> map = (Map<String, String>) obj;
map.clear();
map.putAll(newenv);
}
}
}
String genv = System.getenv(GOOGLE_APPLICATION_CREDENTIALS);
System.out.println(genv);
}
}
I'm trying to write a mock HTTP server for unit tests, I'm using the com.sun.net.httpserver classes for that.
I'm having problem with the encoding of the URL: the query parameters are ISO-8859-1 encoded, but the URI that is passed to the handler (via HttpExchange) is not.
As I can't change the encoding of the original server, I was wondering if there was a way to tell the HttpServer which encoding to use when decoding the URL.
Thanks in advance.
Here is a test program:
package test34;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
public static void main(String[] args) {
try {
MockServer mock = new MockServer();
mock.start(8642);
URL url = new URL("http://localhost:8642/?p="
+ URLEncoder.encode("téléphone", "ISO-8859-1"));
System.out.println(url);
InputStream in = url.openStream();
while (in.read() > 0) {
}
in.close();
mock.stop();
System.out.println(mock.getLastParams().get("p"));
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
And here is the code of the mock server:
package test34;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class MockServer {
private HttpServer httpServer;
private Map<String, String> params;
public void start(int port) {
if (httpServer == null) {
try {
InetSocketAddress addr = new InetSocketAddress(port);
httpServer = HttpServer.create(addr, 0);
httpServer.createContext("/", new HttpHandler() {
#Override
public void handle(HttpExchange exchange) throws IOException {
try {
handleRoot(exchange);
} catch (RuntimeException e) {
throw e;
} catch (IOException e) {
throw e;
}
}
});
httpServer.setExecutor(Executors.newFixedThreadPool(1));
httpServer.start();
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
}
public void stop() {
if (httpServer != null) {
httpServer.stop(10);
httpServer = null;
}
}
public Map<String, String> getLastParams() {
Map<String, String> result = new HashMap<String, String>();
if (params != null) {
result.putAll(params);
}
return result;
}
private void handleRoot(HttpExchange exchange) throws IOException {
URI uri = exchange.getRequestURI();
params = parseQuery(uri.getQuery());
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain;charset=ISO-8859-1");
exchange.sendResponseHeaders(200, 0);
OutputStream stream = exchange.getResponseBody();
try {
Writer writer = new OutputStreamWriter(stream, "ISO-8859-1");
try {
PrintWriter out = new PrintWriter(writer);
try {
out.println("OK");
} finally {
out.close();
}
} finally {
writer.close();
}
} finally {
stream.close();
}
}
private static Map<String, String> parseQuery(String qry)
throws IOException {
Map<String, String> result = new HashMap<String, String>();
if (qry != null) {
String defs[] = qry.split("[&]");
for (String def : defs) {
int ix = def.indexOf('=');
if (ix < 0) {
result.put(def, "");
} else {
String name = def.substring(0, ix);
String value = URLDecoder.decode(
def.substring(ix + 1), "ISO-8859-1");
result.put(name, value);
}
}
}
return result;
}
}
The javadoc of HttpExchange.getQueryString() says it returns "undecoded query string of request URI, or null if the request URI doesn't have one."
If it's not decoded, and since http headers have to be in 7 bit ASCII (ietf.org/rfc/rfc2616.txt) , then you can decode later with URLDecoder.decode(... "ISO-8859-1");
I'm making a small app for accessing gmail and getting messages with attachments.
Properties props = System.getProperties();
props.put("mail.user", login);
props.put("mail.host", pop3Host);
props.put("mail.debug", "false");
props.setProperty("mail.store.protocol", "imaps");
// set this session up to use SSL for IMAP connections
props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// don't fallback to normal IMAP connections on failure.
props.setProperty("mail.imap.socketFactory.fallback", "false");
// use the simap port for imap/ssl connections.
props.setProperty("mail.imap.socketFactory.port", settings.getPop3Port().toString());
props.setProperty("mail.imap.partialfetch", "false");
props.setProperty("mail.imaps.partialfetch", "false");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", settings.getSmtpAuth().toString());
Session session=null;
session = Session.getInstance(props, new GMailAuthenticator(login, password));
Store store = null;
store = session.getStore("imaps");
store.connect();
Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_WRITE);
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
SearchTerm st = new AndTerm(new SubjectTerm(subjectSubstringToSearch), unseenFlagTerm);
// Get some message references
Message [] messages = inbox.search(st);
System.out.println(messages.length + " -- Messages amount");
//Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
ArrayList<String> attachments = new ArrayList<String>();
LinkedList<MessageBean> listMessages = getPart(messages, attachments);
for(String s :attachments) {
System.out.println(s);
}
inbox.setFlags(messages, new Flags(Flags.Flag.SEEN), true);
BufferedReader reader = new BufferedReader (
new InputStreamReader(System.in));
for (int i=0, j=messages.length; i<j; i++) {
messages[i].setFlag(Flags.Flag.SEEN, true);
}
inbox.close(true);
store.close();
return listMessages;
}
private static LinkedList<MessageBean> getPart(Message[] messages, ArrayList<String> attachments) throws MessagingException, IOException {
LinkedList<MessageBean> listMessages = new LinkedList<MessageBean>();
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (Message inMessage : messages) {
attachments.clear();
if (inMessage.isMimeType("text/plain")) {
MessageBean message = new MessageBean(inMessage.getMessageNumber(), MimeUtility.decodeText(inMessage.getSubject()), inMessage.getFrom()[0].toString(), null, inMessage.getSentDate(), (String) inMessage.getContent(), false, null);
listMessages.add(message);
System.out.println("text/plain");
} else if (inMessage.isMimeType("multipart/*")) {
System.out.println("multipart");
Multipart mp = (Multipart) inMessage.getContent();
MessageBean message = null;
System.out.println(mp.getCount());
for (int i = 0; i < mp.getCount(); i++) {
Part part = mp.getBodyPart(i);
if ((part.getFileName() == null || part.getFileName() == "") && part.isMimeType("text/plain")) {
System.out.println(inMessage.getSentDate());
message = new MessageBean(inMessage.getMessageNumber(), inMessage.getSubject(), inMessage.getFrom()[0].toString(), null, inMessage.getSentDate(), (String) part.getContent(), false, null);
} else if (part.getFileName() != null || part.getFileName() != "") {
if ((part.getDisposition() != null) && (part.getDisposition().equals(Part.ATTACHMENT))) {
System.out.println(part.getFileName());
attachments.add(saveFile(MimeUtility.decodeText(part.getFileName()), part.getInputStream()));
if (message != null) {
message.setAttachments(attachments);
}
}
}
}
listMessages.add(message);
}
}
return listMessages;
}
//method for saving attachment on local disk
private static String saveFile(String filename, InputStream input) {
String strDirectory = "D:\\temp\\attachments";
try{
// Create one directory
boolean success = (new File(strDirectory)).mkdir();
if (success) {
System.out.println("Directory: "
+ strDirectory + " created");
}
} catch (Exception e) {//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
String path = strDirectory+"\\" + filename;
try {
byte[] attachment = new byte[input.available()];
input.read(attachment);
File file = new File(path);
FileOutputStream out = new FileOutputStream(file);
out.write(attachment);
input.close();
out.close();
return path;
} catch (IOException e) {
e.printStackTrace();
}
return path;
}
}
MessageBean:
public class MessageBean implements Serializable {
private String subject;
private String from;
private String to;
private Date dateSent;
private String content;
private boolean isNew;
private int msgId;
private ArrayList<String> attachments;
public MessageBean(int msgId, String subject, String from, String to, Date dateSent, String content, boolean isNew, ArrayList<String> attachments) {
this.subject = subject;
this.from = from;
this.to = to;
this.dateSent = dateSent;
this.content = content;
this.isNew = isNew;
this.msgId = msgId;
this.attachments = attachments;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public Date getDateSent() {
return dateSent;
}
public void setDateSent(Date dateSent) {
this.dateSent = dateSent;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public boolean isNew() {
return isNew;
}
public void setNew(boolean aNew) {
isNew = aNew;
}
public int getMsgId() {
return msgId;
}
public void setMsgId(int msgId) {
this.msgId = msgId;
}
public ArrayList<String> getAttachments() {
return attachments;
}
public void setAttachments(ArrayList<String> attachments) {
this.attachments = new ArrayList<String>(attachments);
}
}
I can connect to my mail account and see amount of unseen messages, but it seems that MessageBean doesn't get populated with attachments.
The biggest problem is that i develop my app on a computer that doesn't have an Internet connection. So i build jar, go to computer that has inet, java -jar it and stare at NullPointer exception. I can't debug this crap. Could someone, please, spot where is my mistake.
EDIT
This code works for gmail pop, with another connection obviously.
becuase your list is null when you get elements from it, so you can make new ArrayList object inside MessageBean, and then copy the elements
private List<String> attachments= new ArrayList<String>();
public MessageBean(int msgId, String subject, String from, String to, Date dateSent, String content, boolean isNew, ArrayList<String> attachments) {
this.subject = subject;
this.from = from;
this.to = to;
this.dateSent = dateSent;
this.content = content;
this.isNew = isNew;
this.msgId = msgId;
this.attachments.addAll(attachments);
}
The JavaMail FAQ has debugging tips as well as tips for using Gmail.
What value are you using for pop3host? Hopefully it's "imap.gmail.com" and not "pop.gmail.com".
Note that you can remove all those socket factory properties, you don't need them.
package com.thread.test;
import java.io.File;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;
public class ReadMailThread implements Runnable{
private Thread readMailThread;
private String threadName;
public ReadMailThread() {
readMailThread = new Thread(this);
readMailThread.start();
}
public ReadMailThread(String s) {
threadName = s;
System.out.println("creating thread :: "+threadName);
}
#Override
public void run() {
System.out.println("Thread Started "+threadName);
String saveDirectory = "D:/Ganga/attachment";
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "******#gmail.com", "******");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
/* Message msg = inbox.getMessage(inbox.getMessageCount());
System.out.println("Message Size is "+msg.getSize()/1024 +" KB");
System.out.println("Message Size is "+msg.getSize()/1024*1024 +" MB");
Address[] in = msg.getFrom();
for (Address address : in) {
System.out.println("FROM:" + address.toString());
}
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
System.out.println("SENT DATE:" + msg.getSentDate());
System.out.println("SUBJECT:" + msg.getSubject());
System.out.println("CONTENT:" + bp.getContent());*/
Message[] messages = inbox.getMessages();
for (int i = 0; i < messages.length; i++) {
String contentType = messages[i].getContentType();
String messageContent = "";
String attachFiles = "";
if (contentType.contains("multipart")) {
Multipart multiPart = (Multipart) messages[i].getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
String fileName = part.getFileName();
attachFiles += fileName + ", ";
part.saveFile(saveDirectory + File.separator + fileName);
} else {
messageContent = part.getContent().toString();
}
}
if (attachFiles.length() > 1) {
attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
}
} else if (contentType.contains("text/plain")
|| contentType.contains("text/html")) {
Object content = messages[i].getContent();
if (content != null) {
messageContent = content.toString();
}
}
/*System.out.println(messages[i].getSize() + " bytes long.");
System.out.println(messages[i].getSize()/1024 + " KB long.");
System.out.println(messages[i].getSize()/1024*1024 + " MB long.");
System.out.println(messages[i].getLineCount() + " lines.");
String disposition = messages[i].getDisposition();
if (disposition == null){
//Do Nothing
}else if (disposition.equals(Part.INLINE)) {
System.out.println("This part should be displayed inline");
} else if (disposition.equals(Part.ATTACHMENT)) {
System.out.println("This part is an attachment");
String fileName = messages[i].getFileName();
System.out.println("The file name of this attachment is " + fileName);
}
String description = messages[i].getDescription();
if (description != null) {
System.out.println("The description of this message is " + description);
}*/
}
inbox.close(false);
} catch (Exception mex) {
mex.printStackTrace();
}
}
public void start(){
if(readMailThread == null){
readMailThread = new Thread(this, threadName);
readMailThread.start();
}
}
}
package com.thread.test;
public class MailTest {
public MailTest() {
}
public static void main(String[] args) {
System.out.println("Thread Name :"+Thread.currentThread().getName());
ReadMailThread rmt1=new ReadMailThread("remoteThread1");
ReadMailThread rmt2=new ReadMailThread("remoteThread2");
ReadMailThread rmt3=new ReadMailThread("remoteThread3");
ReadMailThread rmt4=new ReadMailThread("remoteThread4");
ReadMailThread rmt5=new ReadMailThread("remoteThread5");
ReadMailThread rmt6=new ReadMailThread("remoteThread6");
ReadMailThread rmt7=new ReadMailThread("remoteThread7");
ReadMailThread rmt8=new ReadMailThread("remoteThread8");
ReadMailThread rmt9=new ReadMailThread("remoteThread9");
ReadMailThread rmt10=new ReadMailThread("remoteThread10");
rmt1.start();
rmt2.start();
rmt3.start();
rmt4.start();
rmt5.start();
rmt6.start();
rmt7.start();
rmt8.start();
rmt9.start();
rmt10.start();
}
}
package com.equinix.gse.apps.me.tas.app.ec.process.snmp.fivemins;
import com.equinix.gse.apps.me.tas.app.ec.dao.snmp.Snmp5MinsDatabaseReadThread;
import com.equinix.gse.apps.me.tas.core.TasException;
import com.equinix.gse.apps.me.tas.core.util.TasConstants;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* User: vreddy
*/
#Service
public class Snmp5MinsCache {
private static final Logger logger = Logger.getLogger(Snmp5MinsCache.class);
private static final String CLASS_NAME="Snmp5MinsCache";
private static final String SNMP5MINSDATABASEREADTHREAD="snmp5MinsDatabaseReadThread";
#Autowired
private ApplicationContext ac;
public Map<String,Long> getCache() throws TasException{
ExecutorService executor = Executors.newFixedThreadPool(TasConstants.THREAD_POOL_SIZE);
int startCounter = 1;
int endCounter = TasConstants.SNMP_EXDB_BATCH_SIZE;
StringBuilder query = new StringBuilder();
query.append("select CUSTOMER_PORT_ID,IF_INDEX,SWITCH_NAME from (" );
query.append("select CUSTOMER_PORT_ID,IF_INDEX,SWITCH_NAME, ");
query.append("row_number() over (order by CUSTOMER_PORT_ID )r from IXP_PARTICIPANT_VIEW ");
query.append("where PRODUCT_SHORT_NAME ='EC' ) where IF_INDEX is not null and ");
StringBuilder countQuery = new StringBuilder();
countQuery.append("select count(*) from (" );
countQuery.append("select CUSTOMER_PORT_ID,IF_INDEX,SWITCH_NAME, ");
countQuery.append("row_number() over (order by CUSTOMER_PORT_ID )r from IXP_PARTICIPANT_VIEW ");
countQuery.append("where PRODUCT_SHORT_NAME ='EC' ) where IF_INDEX is not null");
Map<String,Long> map = new ConcurrentHashMap<String, Long>();
BasicDataSource dataSource = (BasicDataSource)ac.getBean(TasConstants.DATA_SOURCE);
int recordCount = 0;
Connection conn = null;
ResultSet resultSet = null;
try {
conn = dataSource.getConnection();
resultSet = conn.createStatement().executeQuery(countQuery.toString());
resultSet.next();
recordCount = resultSet.getInt(1);
int timeToExecute= recordCount>0?(recordCount/TasConstants.SNMP_EXDB_BATCH_SIZE)+1:0;
for (int i = 0; i < timeToExecute ; i++) {
StringBuilder prepareWhereClass = new StringBuilder();
prepareWhereClass.append(" r >= ");
prepareWhereClass.append(startCounter);
prepareWhereClass.append(" and r <= ");
prepareWhereClass.append(endCounter);
Snmp5MinsDatabaseReadThread dbRead = (Snmp5MinsDatabaseReadThread) ac.getBean(SNMP5MINSDATABASEREADTHREAD);
((Snmp5MinsDatabaseReadThread)dbRead).setMap(map);
((Snmp5MinsDatabaseReadThread)dbRead).setQuery(query.toString() + prepareWhereClass.toString());
((Snmp5MinsDatabaseReadThread)dbRead).setDataSource(dataSource);
Runnable worker = dbRead;
executor.execute(worker);
startCounter = endCounter + 1;
endCounter = endCounter + TasConstants.SNMP_EXDB_BATCH_SIZE;
}
} catch (Exception e) {
logger.error(e.getMessage(),e);
throw new TasException(CLASS_NAME, TasConstants.METHOD_GETCACHE,e.getMessage(),e);
} finally {
try {
resultSet.close();
conn.close();
} catch (SQLException e) {
logger.error(e.getMessage(),e);
throw new TasException(CLASS_NAME, TasConstants.METHOD_GETCACHE,e.getMessage(),e);
}
}
executor.shutdown();
while (!executor.isTerminated()) {
logger.info("Waiting to process all the thread");
}
logger.info("SNMP 5 mins cache size "+map.size());
return map;
}
}
===========================================
package com.equinix.gse.apps.me.tas.app.ec.process.snmp.fivemins;
import com.equinix.gse.apps.me.tas.app.ec.dao.snmp.Snmp5MinsDatabaseReadThread;
import com.equinix.gse.apps.me.tas.core.TasException;
import com.equinix.gse.apps.me.tas.core.util.TasConstants;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* User: vreddy
*/
#Service
public class Snmp5MinsCache {
private static final Logger logger = Logger.getLogger(Snmp5MinsCache.class);
private static final String CLASS_NAME="Snmp5MinsCache";
private static final String SNMP5MINSDATABASEREADTHREAD="snmp5MinsDatabaseReadThread";
#Autowired
private ApplicationContext ac;
public Map<String,Long> getCache() throws TasException{
ExecutorService executor = Executors.newFixedThreadPool(TasConstants.THREAD_POOL_SIZE);
int startCounter = 1;
int endCounter = TasConstants.SNMP_EXDB_BATCH_SIZE;
StringBuilder query = new StringBuilder();
query.append("select CUSTOMER_PORT_ID,IF_INDEX,SWITCH_NAME from (" );
query.append("select CUSTOMER_PORT_ID,IF_INDEX,SWITCH_NAME, ");
query.append("row_number() over (order by CUSTOMER_PORT_ID )r from IXP_PARTICIPANT_VIEW ");
query.append("where PRODUCT_SHORT_NAME ='EC' ) where IF_INDEX is not null and ");
StringBuilder countQuery = new StringBuilder();
countQuery.append("select count(*) from (" );
countQuery.append("select CUSTOMER_PORT_ID,IF_INDEX,SWITCH_NAME, ");
countQuery.append("row_number() over (order by CUSTOMER_PORT_ID )r from IXP_PARTICIPANT_VIEW ");
countQuery.append("where PRODUCT_SHORT_NAME ='EC' ) where IF_INDEX is not null");
Map<String,Long> map = new ConcurrentHashMap<String, Long>();
BasicDataSource dataSource = (BasicDataSource)ac.getBean(TasConstants.DATA_SOURCE);
int recordCount = 0;
Connection conn = null;
ResultSet resultSet = null;
try {
conn = dataSource.getConnection();
resultSet = conn.createStatement().executeQuery(countQuery.toString());
resultSet.next();
recordCount = resultSet.getInt(1);
int timeToExecute= recordCount>0?(recordCount/TasConstants.SNMP_EXDB_BATCH_SIZE)+1:0;
for (int i = 0; i < timeToExecute ; i++) {
StringBuilder prepareWhereClass = new StringBuilder();
prepareWhereClass.append(" r >= ");
prepareWhereClass.append(startCounter);
prepareWhereClass.append(" and r <= ");
prepareWhereClass.append(endCounter);
Snmp5MinsDatabaseReadThread dbRead = (Snmp5MinsDatabaseReadThread) ac.getBean(SNMP5MINSDATABASEREADTHREAD);
((Snmp5MinsDatabaseReadThread)dbRead).setMap(map);
((Snmp5MinsDatabaseReadThread)dbRead).setQuery(query.toString() + prepareWhereClass.toString());
((Snmp5MinsDatabaseReadThread)dbRead).setDataSource(dataSource);
Runnable worker = dbRead;
executor.execute(worker);
startCounter = endCounter + 1;
endCounter = endCounter + TasConstants.SNMP_EXDB_BATCH_SIZE;
}
} catch (Exception e) {
logger.error(e.getMessage(),e);
throw new TasException(CLASS_NAME, TasConstants.METHOD_GETCACHE,e.getMessage(),e);
} finally {
try {
resultSet.close();
conn.close();
} catch (SQLException e) {
logger.error(e.getMessage(),e);
throw new TasException(CLASS_NAME, TasConstants.METHOD_GETCACHE,e.getMessage(),e);
}
}
executor.shutdown();
while (!executor.isTerminated()) {
logger.info("Waiting to process all the thread");
}
logger.info("SNMP 5 mins cache size "+map.size());
return map;
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
public class MailProcessor implements Runnable {
String downloadDirectory = "C:/tmp/downloads/";
private Message message;
public MailProcessor() {
}
public void run() {
System.out.println("Starting processing a message with thread id"
+ Thread.currentThread().getId());
try {
readMails();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Finished processing a message with thread id"
+ Thread.currentThread().getId());
}
public void readMails() throws MessagingException {
Address[] from = message.getFrom();
System.out.println("-------------------------------");
System.out.println("Date : " + message.getSentDate());
System.out.println("From : " + from[0]);
System.out.println("Subject: " + message.getSubject());
System.out.println("Content :");
processMessageBody(message);
System.out.println("--------------------------------");
}
public void processMessageBody(Message message) {
try {
Object content = message.getContent();
// check for string
// then check for multipart
if (content instanceof String) {
System.out.println(content);
} else if (content instanceof Multipart) {
Multipart multiPart = (Multipart) content;
procesMultiPart(multiPart);
} else if (content instanceof InputStream) {
InputStream inStream = (InputStream) content;
int ch;
while ((ch = inStream.read()) != -1) {
System.out.write(ch);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public void procesMultiPart(Multipart content) {
InputStream inStream = null;
FileOutputStream outStream = null;
try {
for (int i = 0; i < content.getCount(); i++) {
BodyPart bodyPart = content.getBodyPart(i);
Object o;
o = bodyPart.getContent();
if (o instanceof String) {
System.out.println("Text = " + o);
} else if (null != bodyPart.getDisposition()
&& bodyPart.getDisposition().equalsIgnoreCase(
Part.ATTACHMENT)) {
String fileName = bodyPart.getFileName();
System.out.println("fileName = " + fileName);
inStream = bodyPart.getInputStream();
outStream = new FileOutputStream(new File(downloadDirectory
+ fileName));
byte[] tempBuffer = new byte[4096];// KB
int numRead = 0;
while ((numRead = inStream.read(tempBuffer)) != -1) {
outStream.write(tempBuffer);
}
}
// else?
}
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
//
e.printStackTrace();
}
}
if (outStream != null) {
try {
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public MailProcessor setMessage(Message message) {
this.message = message;
return this;
}
}
===========================================================================
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.mail.Message;
public class MailServer {
public static void start(int threadCount,
int frequencyInSecondsToListenMail) {
long lastFinished = System.currentTimeMillis();
do {
if ((System.currentTimeMillis() - lastFinished) / 1000 >= frequencyInSecondsToListenMail) {
readMails(threadCount);
}
} while (true);
}
private static void readMails(int threadCount) {
List<Message> recentMessages = MailUtility.readMessages();
ExecutorService executorService = Executors
.newFixedThreadPool(threadCount);
if(recentMessages == null || recentMessages.isEmpty()){
System.out.println("No messages found.");
}
for (Message message : recentMessages) {
executorService
.execute(new MailProcessor().setMessage(message));
}
executorService.shutdown();
}
public static void main(String[] args) {
MailServer.start(2, 2);
}
}
=======================================================================
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMessage;
import javax.mail.search.FlagTerm;
public final class MailUtility {
public static List<Message> readMessages() {
Properties properties = new Properties();
properties.setProperty("mail.host", "imap.gmail.com");
properties.setProperty("mail.port", "");
properties.setProperty("mail.transport.protocol", "imaps");
Session session = Session.getInstance(properties,
new javax.mail.Authenticator() {
final Properties mailServerProperties = PropertyFileReader
.getProperties("mail-server.properties");
String username = mailServerProperties
.getProperty("username");
String password = mailServerProperties
.getProperty("password");
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Store store;
try {
store = session.getStore("imaps");
store.connect();
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
Message messages[] = inbox.search(new FlagTerm(
new Flags(Flag.SEEN), false));
List<Message> result = new ArrayList<Message>();
for(Message message : messages){
result.add(new MimeMessage((MimeMessage)message));
}
inbox.close(false);
store.close();
return result;
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
==========================================================================
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Flags.Flag;
public class MessagesProcessor implements Runnable {
String downloadDirectory = "C:/tmp/downloads/";
private List<Message> messages;
public MessagesProcessor() {
}
public void run() {
System.out.println("Starting processing " + messages.size()
+ " messages in the thread " + Thread.currentThread().getId());
try {
readMails();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Finished processing " + messages.size()
+ " messages in the thread " + Thread.currentThread().getId());
}
public void readMails() throws MessagingException {
for (Message message : messages) {
Address[] from = message.getFrom();
System.out.println("-------------------------------");
System.out.println("Date : " + message.getSentDate());
System.out.println("From : " + from[0]);
System.out.println("Subject: " + message.getSubject());
System.out.println("Content :");
processMessageBody(message);
System.out.println("--------------------------------");
message.setFlag(Flag.SEEN, true);
}
}
public void processMessageBody(Message message) {
try {
Object content = message.getContent();
// check for string
// then check for multipart
if (content instanceof String) {
System.out.println(content);
} else if (content instanceof Multipart) {
Multipart multiPart = (Multipart) content;
procesMultiPart(multiPart);
} else if (content instanceof InputStream) {
InputStream inStream = (InputStream) content;
int ch;
while ((ch = inStream.read()) != -1) {
System.out.write(ch);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public void procesMultiPart(Multipart content) {
InputStream inStream = null;
FileOutputStream outStream = null;
try {
for (int i = 0; i < content.getCount(); i++) {
BodyPart bodyPart = content.getBodyPart(i);
Object o;
o = bodyPart.getContent();
if (o instanceof String) {
System.out.println("Text = " + o);
} else if (null != bodyPart.getDisposition()
&& bodyPart.getDisposition().equalsIgnoreCase(
Part.ATTACHMENT)) {
String fileName = bodyPart.getFileName();
System.out.println("fileName = " + fileName);
inStream = bodyPart.getInputStream();
outStream = new FileOutputStream(new File(downloadDirectory
+ fileName));
byte[] tempBuffer = new byte[4096];// KB
int numRead;
while ((numRead = inStream.read(tempBuffer)) != -1) {
outStream.write(tempBuffer);
}
}
// else?
}
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
//
e.printStackTrace();
}
}
if (outStream != null) {
try {
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public MessagesProcessor setMessages(List<Message> messages) {
this.messages = messages;
return this;
}
}
============================================================================
import java.io.IOException;
import java.util.Properties;
public final class PropertyFileReader {
public static Properties getProperties(String filename) {
ClassLoader contextClassLoader = Thread.currentThread()
.getContextClassLoader();
Properties properties = new Properties();
try {
properties.load(contextClassLoader.getResourceAsStream(filename));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return properties;
}
}
for(int i=0; i < timeToExecute; i++){
for(int j=0;j<5;j++){
Runnable runner = null;
if(i+1 == timeToExecute){
endIndex = count;
runner = new TaskPrint(startIndex, endIndex, inbox);
} else {
runner = new TaskPrint(startIndex, endIndex, inbox);
}
executor.execute(runner);
startIndex = endIndex + 1;
endIndex = endIndex + 200;
i++;
}
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class DAOUtils {
public static void createTable(){
String createSql = "CREATE TABLE MAILBOX " +
" from VARCHAR(255), " +
" sentDate VARCHAR(255), " +
" subject VARCHAR(255), " +
" message VARCHAR(255)";
Connection con = DBConnection.getInstance().getConnection();
Statement stmt;
try {
stmt = con.createStatement();
stmt.executeUpdate(createSql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void insertRow(MailInfo mes){
Connection conn = DBConnection.getInstance().getConnection();
String sql = "INSERT INTO MAILBOX (from, sentDate, subject, message)" +
"VALUES (?, ?, ?, ?)";
try {
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, mes.getFrom());
preparedStatement.setString(2, mes.getSentDate());
preparedStatement.setString(3, mes.getSubject());
preparedStatement.setString(4, mes.getMessageContent());
preparedStatement.executeUpdate();
} catch (SQLException e){
e.printStackTrace();
}
}
}
===========================================================================
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DBConnection {
private static Connection connect;
private static DBConnection instance;
private DBConnection()
{
final Properties mailServerProperties = PropertyFileReader
.getProperties("mail-server.properties");
String username = mailServerProperties
.getProperty("db_username");
String password = mailServerProperties
.getProperty("db_password");
String ip = mailServerProperties
.getProperty("ip");
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://" +ip +"/database",username,password);
}catch(SQLException e)
{
System.err.println(e.getMessage());
}catch(ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
}
public static DBConnection getInstance()
{
if(instance == null) {
instance = new DBConnection();
}
return instance;
}
public static Connection getConnection(){
return connect;
}
}
=========================================================================
public class MailInfo {
private String sentDate;
private String subject;
private String from;
private String messageContent;
public String getSentDate() {
return sentDate;
}
public void setSentDate(String sentDate) {
this.sentDate = sentDate;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getMessageContent() {
return messageContent;
}
public void setMessageContent(String messageContent) {
this.messageContent = messageContent;
}
}
===========================================================================
mail-server.properties
username = hhh60
password = $test123$
db_username = root
db_password = root
ip = localhost:3305
use_db = false
==========================================================================
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.mail.Message;
public class MailServer {
public static void start(int messageCountForEachThread, int threadCount) {
List<Message> messages = MailUtility.readMessages();
int messageLength = messages.size();
if (messages == null || messages.size() == 0) {
System.out.println("No messages found");
return;
}
System.out.println("Number of messages found " + messageLength);
if (messageLength <= messageCountForEachThread) {
new Thread(new MessagesProcessor().setMessages(messages)).start();
} else {
ExecutorService executorService = Executors
.newFixedThreadPool(threadCount);
int x = messageLength / messageCountForEachThread;
int remaining = messageLength % messageCountForEachThread;
int i = 1;
while (i <= x) {
int startIndex = (i - 1) * messageCountForEachThread;
int endIndex = startIndex + messageCountForEachThread;
List<Message> messagesForEachThread = messages.subList(
startIndex, endIndex);
executorService.execute(new MessagesProcessor()
.setMessages(messagesForEachThread));
i++;
}
int startIndex = i - 1 * messageCountForEachThread;
int endIndex = startIndex + remaining;
List<Message> messagesForEachThread = messages.subList(startIndex,
endIndex);
executorService.execute(new MessagesProcessor()
.setMessages(messagesForEachThread));
executorService.shutdown();
}
}
public static void main(String[] args) {
MailServer.start(2, 2);
}
}
=======================================================================
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;
public final class MailUtility {
public static List<Message> readMessages() {
final Properties mailServerProperties = PropertyFileReader
.getProperties("mail-server.properties");
boolean useDb = Boolean.parseBoolean(mailServerProperties.getProperty("use_db"));
Properties properties = new Properties();
properties.setProperty("mail.host", "imap.gmail.com");
properties.setProperty("mail.port", "");
properties.setProperty("mail.transport.protocol", "imaps");
Session session = Session.getInstance(properties,
new javax.mail.Authenticator() {
String username = mailServerProperties
.getProperty("username");
String password = mailServerProperties
.getProperty("password");
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Store store;
try {
store = session.getStore("imaps");
store.connect();
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
Message messages[] = inbox.search(new FlagTerm(
new Flags(Flag.SEEN), false));
if(useDb){
DAOUtils.createTable();
}
return Arrays.asList(messages);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
================================================================
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
public class MessagesProcessor implements Runnable {
String downloadDirectory = "C:/tmp/downloads/";
boolean useDb;
private List<Message> messages;
public MessagesProcessor() {
final Properties mailServerProperties = PropertyFileReader
.getProperties("mail-server.properties");
useDb = Boolean.parseBoolean(mailServerProperties.getProperty("use_db"));
}
public void run() {
System.out.println("Starting processing " + messages.size()
+ " messages in the thread " + Thread.currentThread().getId());
try {
readMails();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("Finished processing " + messages.size()
+ " messages in the thread " + Thread.currentThread().getId());
}
public void readMails() throws MessagingException {
for (Message message : messages) {
Address[] from = message.getFrom();
System.out.println("-------------------------------");
System.out.println("Date : " + message.getSentDate());
System.out.println("From : " + from[0]);
System.out.println("Subject: " + message.getSubject());
System.out.println("Content :");
processMessageBody(message);
System.out.println("--------------------------------");
if(useDb){
MailInfo info = new MailInfo();
info.setFrom(from[0].toString());
info.setSentDate(message.getSentDate().toString());
info.setSubject(message.getSubject());
info.setMessageContent(message.toString());
DAOUtils.insertRow(info);
}
}
}
public void processMessageBody(Message message) {
try {
Object content = message.getContent();
// check for string
// then check for multipart
if (content instanceof String) {
System.out.println(content);
} else if (content instanceof Multipart) {
Multipart multiPart = (Multipart) content;
procesMultiPart(multiPart);
} else if (content instanceof InputStream) {
InputStream inStream = (InputStream) content;
int ch;
while ((ch = inStream.read()) != -1) {
System.out.write(ch);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public void procesMultiPart(Multipart content) {
InputStream inStream = null;
FileOutputStream outStream = null;
try {
for (int i = 0; i < content.getCount(); i++) {
BodyPart bodyPart = content.getBodyPart(i);
Object o;
o = bodyPart.getContent();
if (o instanceof String) {
System.out.println("Text = " + o);
} else if (null != bodyPart.getDisposition()
&& bodyPart.getDisposition().equalsIgnoreCase(
Part.ATTACHMENT)) {
String fileName = bodyPart.getFileName();
System.out.println("fileName = " + fileName);
inStream = bodyPart.getInputStream();
File f = new File(downloadDirectory + fileName);
if(!f.exists()){
System.out.println("Downloading file : " + fileName + " .............");
outStream = new FileOutputStream(f);
byte[] tempBuffer = new byte[4096];// KB
int numRead = 0;
while ((numRead = inStream.read(tempBuffer)) != -1) {
outStream.write(tempBuffer);
}
System.out.println("Download completed");
} else {
System.out.println("Given file name already exists");
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
if (inStream != null) {
try {
inStream.close();
} catch (IOException e) {
//
e.printStackTrace();
}
}
if (outStream != null) {
try {
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public MessagesProcessor setMessages(List<Message> messages) {
this.messages = messages;
return this;
}
}
==================================================================
import java.io.IOException;
import java.util.Properties;
public final class PropertyFileReader {
public static Properties getProperties(String filename) {
ClassLoader contextClassLoader = Thread.currentThread()
.getContextClassLoader();
Properties properties = new Properties();
try {
properties.load(contextClassLoader.getResourceAsStream(filename));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return properties;
}
}
package Test.TestArtifact;
import java.time.LocalDate;
import java.util.Date;
import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.internet.MimeBodyPart;
public class TaskPrint implements Runnable{
private Message messages;
private Date startDate;
public TaskPrint(Date startDate, Message messages){
this.startDate = startDate;
this.messages = messages;
}
public void run() {
try {
if(messages.getSentDate().after(startDate) && messages.getSentDate().before(startDate)){
Address[] in = messages.getFrom();
String contentType = messages.getContentType();
String messageContent = "";
String attachFiles = "";
if(contentType.contains("multiport")){
Multipart multipart = (Multipart) messages.getContent();
int noOfParts = multipart.getCount();
for(int partCount =0; partCount < noOfParts; partCount++){
MimeBodyPart mime = (MimeBodyPart) multipart.getBodyPart(partCount);
if(Part.ATTACHMENT.equalsIgnoreCase(mime.getDisposition())){
String fileName = mime.getFileName();
attachFiles += fileName + ",";
if(fileName.endsWith("=")){
mime.saveFile("C:\\tmp\\downloads" + fileName);
} else {
messageContent = mime.getContent().toString();
}
}
if(attachFiles.length() > 1){
attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
} else if(contentType.contains("text/plain") || contentType.contains("text/html")) {
Object content = messages.getContent();
if(content != null){
messageContent = content.toString();
}
}
}
}
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
package Test.TestArtifact;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
public class ExecutorServiceExample {
public static void main(String[] args) throws MessagingException {
ExecutorServiceExample eService = new ExecutorServiceExample();
Folder inbox = eService.getInboxFolder();
int count = eService.getMailCount();
Message messages = inbox.getMessage(count);
System.out.println("Total Mail count = " + count);
ExecutorService executor = Executors.newFixedThreadPool(5);
int waitTime = 1000000;
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date startDate;
Date endDate;
try {
startDate = formatter.parse("2015-10-01");
endDate = formatter.parse("2015-10-31");
while(startDate.before(endDate)){
for(int j=0;j<5;j++){
Runnable runner = new TaskPrint(startDate,messages);
executor.execute(runner);
}
}
} catch (ParseException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(waitTime);
executor.shutdown();
executor.awaitTermination(waitTime, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(0);
}
private Folder getInboxFolder() throws MessagingException{
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imap");
Session session = Session.getInstance(props);
Store store = session.getStore("imap");
store.connect("", "", "");
store.getFolder("INBOX").open(Folder.READ_ONLY);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
return inbox;
}
private int getMailCount() throws MessagingException{
int count = getInboxFolder().getMessageCount();
return count;
}
}
import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
public class ExecutorServiceExample {
public static void main(String[] args) throws MessagingException {
ExecutorServiceExample eService = new ExecutorServiceExample();
Folder inbox = eService.getInboxFolder();
Message[] messages = inbox.getMessages();
System.out.println("Total Mail count = " + messages.length);
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
for(Message message: messages){
Runnable runner = new TaskPrint(message);
executor.execute(runner);
}
System.out.println("Maximum threads inside pool " + executor.getMaximumPoolSize());
executor.shutdown();
try {
Thread.sleep(1000);
executor.shutdown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private Folder getInboxFolder() throws MessagingException{
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getInstance(props,null);
Store store = session.getStore();
store.connect("imap.gmail.com", "aaa#gmail.com", "aaaaa");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
return inbox;
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
public class TaskPrint implements Runnable{
private Message message;
public TaskPrint(Message message){
this.message = message;
}
public void run() {
try {
Multipart multipart = (Multipart) message.getContent();
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
if(!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition()) &&
bodyPart.getFileName() != null) {
continue; // dealing with attachments only
}
System.out.println("Message subject " + message.getSubject());
InputStream is = bodyPart.getInputStream();
File f = new File("c:/tmp/" + bodyPart.getFileName());
FileOutputStream fos = new FileOutputStream(f);
byte[] buf = new byte[4096];
int bytesRead;
while((bytesRead = is.read(buf))!=-1) {
fos.write(buf, 0, bytesRead);
}
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch(MessagingException e){
e.printStackTrace();
}
}
}