I have searched for the solution of my problem as much as I could.
My App runs on Glassfish v3.This app sends a message to Glassfish JMS queue and this message is supposed to be read by a standalone client on the same host but outside Glassfish JVM.
I have written standalone client java code - have included appserv-rt.jar and gf-client.jar from Glassfish installation directory.
This client code is unable to receive the message.System out statements print till "got consumer".After that nothing happens.
If I change the name of the queue-I get an error saying Queue not found.So it seems client code is able to find queue but it does not receive any messages.What do I need to include in my client code?
Here is my Java class :-
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Queue;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.Category;
import domain.RedirectFile;
public class ZblBulkUploadThread implements Runnable,MessageListener{
private static final Category log = Category.getInstance(ZblBulkUploadThread.class) ;
private Queue queue;
public void run()
{
try
{
System.out.println(" inside try") ;
InitialContext jndiContext = null;
MessageConsumer messageConsumer=null;
jndiContext = new InitialContext();
System.out.println(" got context ") ;
ConnectionFactory connectionFactory = (ConnectionFactory)jndiContext.
lookup("jms/SimpleConnectionFactory");
System.out.println("got connectionfactory") ;
Connection connection = connectionFactory.createConnection();
System.out.println("got connection") ;
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
queue = (Queue)jndiContext.lookup("jms/SimpleQueue") ;
System.out.println("got queue"+queue.getQueueName()) ;
messageConsumer = session.createConsumer(queue);
System.out.println(" selector "+messageConsumer.getMessageSelector()) ;
System.out.println("got consumer") ;
Message message = messageConsumer.receive() ;
System.out.println("Message is "+message) ;
System.out.println("destination is "+message.getJMSDestination()) ;
ObjectMessage om = ((ObjectMessage)message) ;
try
{
RedirectFile file = (RedirectFile)om.getObject() ;
log.debug("filePath "+file.getFilePath()) ;
log.debug(" userName "+file.getUserName()) ;
log.debug(" mode is "+file.getMode()) ;
System.out.println("filePath "+file.getFilePath()) ;
System.out.println(" userName "+file.getUserName()) ;
System.out.println(" mode is "+file.getMode()) ;
}
catch(Exception ex)
{
log.error("ERROR "+ex.getMessage()) ;
ex.printStackTrace() ;
}
log.debug("session created") ;
}
catch(Exception ex)
{
ex.printStackTrace() ;
log.error("Error "+ex.getMessage()) ;
}
}
public void onMessage(Message message)
{
System.out.println("Message received "+message) ;
}
public static void main(String[] args)
{
ZblBulkUploadThread zbut = new ZblBulkUploadThread() ;
new Thread(zbut).start() ;
}
}
try: connection.start(); somewhere before the receive call.
Related
I am trying to connect on GCP memorystore from teh same VM . But getting
{"timestamp":"2018-07-26T20:02:48.127+0000","status":500,"error":"Internal Server Error","message":"Error connecting to Jedis pool","path":"/ds/initiate"} , when i am trying to connect redis directly from VM it is connected and can PING and PONG , so redis is connected . but i am getting the above error. When in install redis locally on VM and then run the application , the application works just fine, but not when i try connecting from same VM to memorystore ip , please help
package com.example.demo;
import java.io.IOException;
import java.net.SocketException;
import java.util.Properties;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
#Controller
#RequestMapping("/ds")
public class ControllerMain {
#GetMapping("/initiate")
public void checkhe(HttpServletRequest req , HttpServletResponse resp) throws IOException {
try {
JedisPool jedisPool = (JedisPool) req.getServletContext().getAttribute("jedisPool");
if (jedisPool == null) {
throw new SocketException("Error connecting to Jedis pool");
}
Long visits;
try (Jedis jedis = jedisPool.getResource()) {
visits = jedis.incr("visits");
}
resp.setStatus(HttpServletResponse.SC_OK);
resp.getWriter().println("Visitor counter: " + String.valueOf(visits));
} catch (Exception e) {
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
}
private Properties config = new Properties();
private JedisPool createJedisPool() throws IOException {
String host;
Integer port;
config.load(
Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("application.properties"));
host = config.getProperty("redis.host");
port = Integer.valueOf(config.getProperty("redis.port", "6379"));
JedisPoolConfig poolConfig = new JedisPoolConfig();
// Default : 8, consider how many concurrent connections into Redis you will need under load
poolConfig.setMaxTotal(128);
return new JedisPool(poolConfig, host, port);
}
}
Here is my code:
package pushnotiruntest;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Send extends Thread {
String name = "";
String app_type = "";
private static final String EXCHANGE_NAME = "topic_exchange";
public void run()
{
ConnectionFactory connFac = new ConnectionFactory();
connFac.setHost("localhost");
try {
Connection conn = connFac.newConnection();
Channel channel = conn.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
for(int j=1; j<=20000; j++)
{
//randomWait();
String routingKey = j+"."+"update"+"."+app_type;
String msg = name;
channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes("UTF-8"));
System.out.println("Sent " + routingKey + " : " + msg + "");
}
channel.close();
conn.close();
} catch (IOException ex) {
Logger.getLogger(Send.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Exception1 :--"+ex);
} catch (TimeoutException ex) {
Logger.getLogger(Send.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Exception 2:--"+ex);
}
}
/*void randomWait()
{
try {
Thread.currentThread().sleep((long)(3*Math.random()));
} catch (InterruptedException x) {
System.out.println("Interrupted!");
}
}*/
public static void main(String[] args) {
// TODO code application logic here
Send test1 = new Send();
test1.name = "Hello ANDROID";
test1.app_type = "ANDROID";
Send test2 = new Send();
test2.name = "Hello IOS";
test2.app_type = "IOS";
Send test3 = new Send();
test3.name = "Hello WINDOWS";
test3.app_type = "WINDOWS";
test1.start();
test2.start();
test3.start();
}
}
//javac -cp amqp-client-4.0.2.jar Send.java Recv.java
//java -cp .;amqp-client-4.0.2.jar;slf4j-api-1.7.21.jar;slf4j-simple-1.7.22.jar Recv
//java -cp .;amqp-client-4.0.2.jar;slf4j-api-1.7.21.jar;slf4j-simple-1.7.22.jar
Send
I am writing a code in Java (message broker used is RabbitMQ) where I want to store messages send by the producers in a single queue with different routing key.
And fetch that messages with respect to there pattern for different consumers
that matches the routing key pattern. (I am using Topic exchange for the pattern matching).
If you need two consumers, you have to use two queues.
The binding if from exchange to queue(s) you cannot decide the routing key during the subscription.
You can bind more routing key to the same queue, but you can't consume it filtering by the key.
I think you need something like:
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
channel.queueDeclare(QUEUE_NAME_1, true, false, false, null);
channel.queueDeclare(QUEUE_NAME_2, true, false, false, null);
channel.queueBind(QUEUE_NAME_1, EXCHANGE_NAME, "my.rk.1");
channel.queueBind(QUEUE_NAME_2, EXCHANGE_NAME, "my.rk.2");
channel_consumer_1.basicConsume(QUEUE_NAME_1, false, new DefaultConsumer(channel_consumer) {...}
....
channel_consumer_2.basicConsume(QUEUE_NAME_2, false, new DefaultConsumer(channel_consumer) {...}
I'd like to create a consumer which stacks arriving messages, and waits:
until n messages have arrived.
t seconds are elapsed.
to process the whole stack of messages.
Pre-fetching is not what I'm looking for. What I really need is to process messages together.
class MyListener(stomp.ConnectionListener):
def on_message(self, headers, body):
print ("Just received ONE message\n"
"I should wait for n-1 others\n"
"or t seconds before processing")
here an example
import java.util.LinkedList;
import java.util.List;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQMessageConsumer;
public class SimpleConsumerClientAcknowledge {
public static void main(String[] args) throws JMSException {
List<TextMessage> messages = new LinkedList<>();
Connection conn = null;
try {
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(
"tcp://localhost:61617?jms.prefetchPolicy.all=200");
conn = cf.createConnection("admin", "admin");
Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session
.createConsumer(session.createQueue("Q"));
conn.start();
TextMessage msg = null;
// MAX_MESSAGES have to be < prefetchSize / 2 -->
// jms.prefetchPolicy.all=200
// Once the broker has dispatched a prefetch limit number of
// messages to a consumer it will not dispatch any more messages to
// that consumer until the consumer has acknowledged at least 50% of
// the prefetched messages
int MAX_MESSAGES = 100;
long MAX_WAIT = 60000;
long millis = System.currentTimeMillis();
while ((msg = (TextMessage) consumer.receive(5000)) != null) {
if (msg != null) {
messages.add(msg);
}
if (messages.size() == MAX_MESSAGES || (System.currentTimeMillis() - millis >= MAX_WAIT)) {
millis = System.currentTimeMillis();
treatMessages(messages);
// because session is created with
// Session.CLIENT_ACKNOWLEDGE as an acknowledgeMode consumer
// need to acknowledge manually received messages
consumer.acknowledge();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
}
}
}
}
private static void treatMessages(List<TextMessage> messages) {
// TODO Auto-generated method stub
messages.clear();
}
}
I am new to JMS coding. I am trying to create message from stand-alone java client which creates and send the message to queue and message driven bean is used for further processing of the messages.
I referred the following guidelines :
http://techtipsjava.blogspot.de/2013/05/jms-on-glassfish-queue-and-topic-with.html
I am using Glassfish application server (3.1). And setup everything to create JMS message from stand-alone java client.
Here is my code:
Client
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class TruckMonitor {
public static void main(String args[]) {
try {
// Provide the details of remote JMS Client
Properties props = new Properties();
props.put(Context.PROVIDER_URL, "mq://localhost:7676");
// Create the initial context for remote JMS server
InitialContext cntxt = new InitialContext(props);
System.out.println("Context Created");
// JNDI Lookup for QueueConnectionFactory in remote JMS Provider
QueueConnectionFactory qFactory = (QueueConnectionFactory)cntxt.lookup("OmazanQueueConnectionFactory");
// Create a Connection from QueueConnectionFactory
Connection connection = qFactory.createConnection();
System.out.println("Connection established with JMS Provide ");
connection.start();
// Initialise the communication session
Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
// Create the message
TextMessage message = session.createTextMessage();
message.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
message.setText(getMessage());
// JNDI Lookup for the Queue in remote JMS Provider
Queue queue = (Queue)cntxt.lookup("OmazanQueue");
// Create the MessageProducer for this communication
// Session on the Queue we have
MessageProducer mp = session.createProducer(queue);
// Send the message to Queue
mp.send(message);
System.out.println("Message Sent: " + getMessage());
// Make sure all the resources are released
mp.close();
session.close();
cntxt.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static String getMessage() {
String msg = null;
StringBuffer sbExceptionEvent = new StringBuffer("<exceptionEvent>");
sbExceptionEvent.append("</exceptionEvent>");
msg = sbExceptionEvent.toString();
return msg;
}
}
Message Driven Bean:
import java.util.Properties;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/** * Message-Driven Bean implementation class for: OmazanMDBean*/
#MessageDriven(
activationConfig = {
#ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
#ActivationConfigProperty(propertyName = "destination", propertyValue = "OmazanQueue"),
#ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
},
mappedName = "OmazanQueue")
public class OmazanMDBean implements MessageListener {
/**
* Default constructor.
* #throws NamingException
* #throws JMSException
*/
public OmazanMDBean() {
super();
}
/**
* #see MessageListener#onMessage(Message)
*/
public void onMessage(Message message) {
System.out.println("Inside omMessage");
try {
message.acknowledge();
} catch (Exception e) {
e.printStackTrace();
}
TextMessage txtMessage = (TextMessage) message;
try {
System.out.println(txtMessage.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
}
The problem is: onMessage() is not getting invoked. Did I miss anything? Please help me.
I guess if you remove #ActivationConfigProperty(propertyName = "destination", propertyValue = "OmazanQueue") from you MessageDrivenBean it will work since you have already used mappedName = "OmazanQueue"
I have solved the problem. You need to create an instance of SSLEngine and add it to the pipeline of handlers for each clinent request. I have done this by adding the handler in the channelConnected event and removing the ssl handler in the channel disconnected. This make sure for each channel connected it will be added new.
Below is the code of the handler. Is this the right approach for doing persistent socket connection with SSL support?
package server;
import static org.jboss.netty.buffer.ChannelBuffers.dynamicBuffer;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.handler.ssl.SslHandler;
import ssl.SslContextFactory;
import ssl.SslKeyStore;
public class ServerHandler extends SimpleChannelHandler {
private static final String ECHORES = "0057081082200000000000000400000000000000070612201399966400301";
#Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
System.out.println("Inside ServerHandler.messageReceived");
ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
ChannelBuffer temp = dynamicBuffer();
temp.writeBytes(buffer);
if (temp.readableBytes() >= 4) {
byte messageLen[] = new byte[4];
temp.readBytes(messageLen);
int len = Integer.parseInt(new String(messageLen));
System.out.println("Length of the message is : " + len);
if (temp.readableBytes() >= len) {
byte[] message = new byte[len];
temp.readBytes(message);
System.out.println("Input message is : " + new String(message));
Channel channel = e.getChannel();
buffer = ChannelBuffers.copiedBuffer(ECHORES.getBytes());
ChannelFuture future = channel.write(buffer);
future.addListener(ChannelFutureListener.CLOSE);
}
}
}
#Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
e.getCause().printStackTrace();
Channel channel = e.getChannel();
channel.close();
}
#Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
String file = "test.jks";
SSLContext sslCtx = SslContextFactory.getServerContext(new SslKeyStore(file));
final SSLEngine sslEngine =sslCtx.createSSLEngine();
sslEngine.setNeedClientAuth(false);
sslEngine.setUseClientMode(false);
final SslHandler sslHandler = new SslHandler(sslEngine);
ctx.getPipeline().addFirst("ssl", sslHandler);
ChannelFuture handshakeFuture = sslHandler.handshake();
handshakeFuture.addListener(new ChannelFutureListener() {
#Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
System.out.println("SSL/TLS session established");
System.out.println("Your session is protected by "+ sslHandler.getEngine().
getSession().getCipherSuite() + " cipher suite.\n");
} else {
future.getChannel().close();
}
}
});
}
#Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
System.out.println("Inside ServerHandler.channelDisconnected");
ctx.getPipeline().remove("ssl");
}
}
I am getting the following exception while using netty with ssl. My first transaction and handshake goes fine. If I send a new message to teh server again I am getting this exception.
"javax.net.ssl.SSLException: SSLEngine is closing/closed"
What could be going wrong here. How to keep the esatablished TLS/SSL session? This error happens at org.jboss.netty.handler.ssl.SslHandler.handshake(SslHandler.java:358).
Intention is to keep the server running with a persistent TLS socket connection , so that clients can send messages.
-TK