When does the TransportEvent get fired in Java mail? - java

I am trying to get the Message-Id of the sent message by using listeners.
I am implementing
javax.mail.event.TransportListener with concrete methods given in code sample.
It listens to javax.mail.event.TransportEvent which gets generated when void javax.mail.Transport.sendMessage(.....) is called.
To my surprise I get none of the method gets called when I actually send the mail..??? When does it actually get called ? Do I need to add any wait time after calling sendMessage(..)??
Doesn't it happen in real time ?
#Override
public void messageDelivered(TransportEvent e)
{
try {
System.out.println(e.getMessage().getHeader("Message-Id")[0]);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
#Override
public void messageNotDelivered(TransportEvent e)
{
try {
System.out.println(e.getMessage().getHeader("Message-Id")[0]);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
#Override
public void messagePartiallyDelivered(TransportEvent e)
{
try {
System.out.println(e.getMessage().getHeader("Message-Id")[0]);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

Did you register your listener with the Transport instance that's being used to send the message? Remember that the static Transport.send() method creates its own Transport instance that you never see.

Related

Why does my RMI server exit before a client can connect?

After starting the program to run on the specified port, the program terminates before a client is able to connect to it.
try {
StudentService obj = new StudentService();
Registry r = LocateRegistry.createRegistry(4200);
r.bind("localhost", obj);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AlreadyBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This is the is the method that is located in the StudentService class that has been overridden.
int multiply(int s, int b) throws RemoteException {
return s * b;
}
From the documentation,
UnicastRemoteObject.exportObject exports the supplied remote object to receive incoming remote method invocations on an anonymous TCP port and returns the stub for the remote object to pass to clients
So, there is no surprise your program ended without listening for incoming connections. This should work.
try {
StudentService obj = new StudentService();
UnicastRemoteObject.exportObject(obj, 0);
Registry r = LocateRegistry.createRegistry(4200);
r.bind("localhost", obj);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AlreadyBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Java Advanced Reflection

So I have class Foo in package com.one.
//package com.one
class Foo{
protected static void a(){
//...
}
}
and class Bar in package com.two extending Foo
//package com.two
class Bar extends Foo{
//...
}
Can I use reflection, if I'm inside com.two, to make a() not protected, and then call it?
Yes you can. Just grab this method and use setAccessible with true.
To grab this method you can't use setDeclaredMethod nor getMethod from subclass, because it wasn't declared there or is not public. Easiest way to get it is to do it via superclass like
Method method = Foo.class.getDeclaredMethod("a");
or
Method method = Bar.class.getSuperclass().getDeclaredMethod("a");
// ^^^^^^^^^^^^^ assuming that `a` is declared in superclass,
// it is possible that you may want to use
// `getSuperclass` few more times
then you can just call
method.setAccessible(true);
to change its accessibility and use it
method.invoke(null);//static methods require no instance.
Yes, you should be able to use reflection for it. Write below code:
try {
Method method = Foo.class.getDeclaredMethod("a");
method.setAccessible(true);
try {
method.invoke(null);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Different behavior when using JOptionPane in Windows7 vs OS X

Whenever I run the below code on a Windows 7 machine, I get odd artifacts above one of the buttons.
Typically it's a straight line above whatever button is highlighted by default and as tab over the other buttons I get a straight line above the other buttons as well.
It also looks like I get a line at the very top of the Window. When I've witnessed these same artifacts before in different java UI's I've worked on I've had to do object.setBorder(BorderFactory.createLineBorder(Color.someMatchingColor)). This obviously isn't an option if I'm doing the JOptionPane method.
Is there some known issue with java?
public class TestProj {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JOptionPane.showConfirmDialog(null, "Test");
}
}

Android - Facebook: an active access token must be used to query information about the current user

i'm using this code:
if(!facebook.isSessionValid()) {
try {
String jsonUser = facebook.request("me");
obj = Util.parseJson(jsonUser);
String id = obj.optString("id");
String name = obj.optString("name");
tv1.setText("sodfnsdf");
} catch (MalformedURLException e) {
tv1.setText("1");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
tv1.setText("2");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FacebookError e) {
tv1.setText("3");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
tv1.setText("4");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
tv1.setText("Error");
}
i've searched online and in stackoverflow, but cannot find the answer to my question, i only found very similiar questions but different in some ways..
Hey you need to call facebook.authorize method
if(!facebook.isSessionValid()) {
facebook.authorize() }
For more information you can refer this link. I think it will help you.
Visit http://www.techrepublic.com/blog/app-builder/integrate-facebook-logins-in-your-android-app/296

getInputStream in Async Task

I have an Activity which calls an Async task to accept socket connections through a predefined InetAddress.
The Async task calls another Async task to listen for messages in. but it hangs on the get input stream
I have been racking my brain for hours and cannot work out why it is hanging...any help please.
public void startSocketListener(InetAddress groupOwnerAddress) {
// TODO Auto-generated method stub
AcceptClientThread accept;
try {
accept = new AcceptClientThread();
accept.execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class AcceptClientThread extends AsyncTask<Void, String, String>{
public AcceptClientThread() throws IOException{
}
#Override
protected void onCancelled() {
// TODO Auto-generated method stub
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("CONNECTION ERR", "Could not close serverSocket " + e.toString());
}
super.onCancelled();
}
#Override
protected String doInBackground(Void... params) {
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
Log.e("CONNECTION ERR","Could not listen on port: " + port);
onCancelled();
}
while (listening){
try {
Log.i("CONNECTION", "AWAITING CONNECTION TO CLIENT");
Socket newSocket = serverSocket.accept();
Log.i("CONNECTION", "CONNECTED TO CLIENT");
ListenerThread lThread = new ListenerThread(newSocket);
lThread.execute("Do it");
Log.i("CONNECTION", "ACCEPTED CLIENT");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
onProgressUpdate("could not accept client");
}
}
Log.i("CONNECTION", "close socket");
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "table connected";
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
// received data is first element in the String
//Toast.makeText(KitchenActivity.this, values[0], Toast.LENGTH_SHORT).show();
}
}
public class ListenerThread extends AsyncTask<String, Order, Void> {
private Socket socket;
ObjectInputStream ois;
public ListenerThread(Socket socket){
this.socket = socket;
try {
ois = new ObjectInputStream(this.socket.getInputStream()); //hangs here
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
According to The Docs
Threading rules
There are a few threading rules that must be followed
for this class to work properly:
The AsyncTask class must be loaded on the UI thread. This is done
automatically as of JELLY_BEAN. The task instance must be created on
the UI thread.
You can handle this by calling your new Thread in the onPostExecute() of your first AsyncTask after your accept() is successful, as onPostExecute() runs on the UI
Also, without looking at it more, I believe that you would want to break out of your while loop in the first task after it accepts the request. Then when you need to make another connection you create a new instance of this task in your UI and execute it again. I'm not positive about that last part without looking at it longer but that seems right
I'm not sure, but I guess you only can call AsyncTask.execute() from UI-thread, and you are trying to do it from doInBackground(), which runs in separate thread.

Categories

Resources