Spring配置文件
class="org.springframework.jndi.JndiTemplate">
org.exolab.jms.jndi.InitialContextFactory
tcp://localhost:3035
class="org.springframework.jndi.JndiObjectFactoryBean">
JmsQueueConnectionFactory
class="org.springframework.jndi.JndiObjectFactoryBean">
测试
class="org.springframework.jms.core.JmsTemplate">
30000
class="com.topwalk.jms.SenderMessage4Spring">
class="com.topwalk.jms.ReceiverManager4Spring">
源代码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
public class ReceiveUseSpring
{
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate)
{
this.jmsTemplate = jmsTemplate;
}
public JmsTemplate getJmsTemplate()
{
return jmsTemplate;
}
public static void main(String[] args)
{
try
{
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[]
{ "applicationContext.xml" });
System.out.println("Classpath loaded");
ReceiveUseSpring jmsReceive = (ReceiveUseSpring) appContext
.getBean("jmsReceive");
// Destination bean =
// (Destination)appContext.getBean("receiveDestination");
// jmsReceive.getJmsTemplate().setDefaultDestination(bean);
jmsReceive.receiveMesage();
System.out.println("Message sent using Spring JMS.");
} catch (Exception e)
{
e.printStackTrace();
}
}
public String receiveMesage()
{
TextMessage massage = (TextMessage) jmsTemplate.receive();
try
{
return massage.getText();
} catch (JMSException e)
{
e.printStackTrace();
return null;
}
}
public Serializable getObject()
{
ObjectMessage massage = (ObjectMessage) jmsTemplate.receive();
try
{
return massage.getObject();
} catch (JMSException e)
{
e.printStackTrace();
return null;
}
}
public void getFile(String fileName)
{
StreamMessage message = (StreamMessage) jmsTemplate.receive();
try
{
OutputStream os = new FileOutputStream(new File(fileName));
byte[] b = new byte[1];
while (message.readBytes(b) != -1)
{
os.write(b);
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Serializable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class SenderUseSpring
{
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate)
{
this.jmsTemplate = jmsTemplate;
}
public static void main(String[] args)
{
try
{
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[]
{ "applicationContext.xml" });
System.out.println("Classpath loaded");
SenderUseSpring jmsSender = (SenderUseSpring) appContext
.getBean("jmsSender");
jmsSender.sendMesage("sss");
System.out.println("Message sent using Spring JMS.");
} catch (Exception e)
{
e.printStackTrace();
}
}
public void sendMesage(String string)
{
final String str = string;
jmsTemplate.send(new MessageCreator()
{
public Message createMessage(Session session) throws JMSException
{
TextMessage message = session.createTextMessage();
message.setText(str);
return message;
}
});
}
public void sendObject(Serializable object)
{
final Serializable ob = object;
jmsTemplate.send(new MessageCreator()
{
public Message createMessage(Session session) throws JMSException
{
ObjectMessage message = session.createObjectMessage();
message.setObject(ob);
return message;
}
});
}
public void sendFile(String fileName)
{
final String name = fileName;
jmsTemplate.send(new MessageCreator()
{
public Message createMessage(Session session) throws JMSException
{
StreamMessage message = session.createStreamMessage();
InputStream is;
try
{
is = new FileInputStream(new File(name));
byte[] b = new byte[2];
int i = 0;
while (is.read(b) != -1)
{
message.writeBytes(b);
}
} catch (Exception e)
{
e.printStackTrace();
}
return message;
}
});
}
}