Kaa: When onStarted() was called in main? - kaa

Like to understand how onStarted() is being called. Does kaaClient.start() executed onStarted()? I tried to debug with ecplise and found it always executed "sleepForSeconds(MAX_SECONDS_TO_INIT_KAA);" before going to "LOG.info("--= Kaa client started =--")" but I can't tell when onStarted() was executed.
public static void main(String[] args) {
LOG.info("--= Data collection demo started =--");
/*
* Create a Kaa client with the Kaa desktop context.
*/
KaaClient kaaClient = Kaa.newClient(new DesktopKaaPlatformContext(), new SimpleKaaClientStateListener() {
#Override
public void onStarted() {
LOG.info("--= Kaa client started =--");
}
#Override
public void onStopped() {
LOG.info("--= Kaa client stopped =--");
}
}, true);
/*
* Set record count strategy for uploading every log record as soon as it is created.
*/
kaaClient.setLogUploadStrategy(new RecordCountLogUploadStrategy(LOGS_DEFAULT_THRESHOLD));
/*
* Displays endpoint's configuration and change sampling period each time configuration will be updated.
*/
kaaClient.addConfigurationListener(new ConfigurationListener() {
#Override
public void onConfigurationUpdate(Configuration configuration) {
LOG.info("--= Endpoint configuration was updated =--");
displayConfiguration(configuration);
Integer newSamplePeriod = configuration.getSamplePeriod();
if ((newSamplePeriod != null) && (newSamplePeriod > 0)) {
changeMeasurementPeriod(kaaClient, newSamplePeriod);
} else {
LOG.warn("Sample period value (= {}) in updated configuration is wrong, so ignore it.", newSamplePeriod);
}
}
});
/*
* Start the Kaa client and connect it to the Kaa server.
*/
kaaClient.start();
sleepForSeconds(MAX_SECONDS_TO_INIT_KAA);
/*
* Start periodical temperature value generating and sending results to Kaa node
*/
startMeasurement(kaaClient);
LOG.info("*** Press Enter to stop sending log records ***");
waitForAnyInput();
/*
* Stop generating and sending data to Kaa node
*/
stopMeasurement();
/*
* Stop the Kaa client and release all the resources which were in use.
*/
kaaClient.stop();
displayResults();
LOG.info("--= Data collection demo stopped =--");
}

Method onStarted() called in the start() method. For details see AbstaractKaaClient.

Related

Application Insights Telemetry filtering is not working

I have already followed the guide here. I have tried both the config and "in code" approach of initializing and registering our telemetry processor. My goal is to filter out some HTTP responses so that those don't make their way to the sampled data. I haven't had any success. While our processor is initialized on app start, the Process method is never hit. Also, I already made sure that there is an InstrumentationKey in the config and that I'm using the correct key. What else am I missing?
This is what I have:
public class MyTelemetryProcessor : ITelemetryProcessor
{
private ITelemetryProcessor Next { get; set; }
// You can pass values from .config
public string MyParamFromConfigFile { get; set; }
// Link processors to each other in a chain.
public MyTelemetryProcessor(ITelemetryProcessor next)
{
this.Next = next; <-- this is always hit indicating this processor is active
}
public void Process(ITelemetry item)
{
// To filter out an item, just return
if (!OKtoSend(item)) { return; } <-- breakpoint here is never hit
// Modify the item if required
ModifyItem(item);
this.Next.Process(item);
}
private bool OKtoSend(ITelemetry item) <-- and consequently this method is never hit
{
var request = item as RequestTelemetry; <-- breakpoint here is never hit
// some more code goes here
return request.Success.GetValueOrDefault(false);
}
// Example: replace with your own modifiers.
private void ModifyItem(ITelemetry item)
{
item.Context.Properties.Add("app-version", "1." + MyParamFromConfigFile);
}
}
And this is how it is registered. I can see this being hit during debugging when the app starts up:
var builder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder;
builder.Use((next) => new MyTelemetryProcessor (next));
builder.Build();
In aspnetcore, my solution was to use :
services.AddApplicationInsightsTelemetryProcessor(typeof(BasicTelemetryFilter));
(using the regular CreateWebHostBuilder :
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>();
)

High CPU usage on PI on kaa client start

When I start the KAA client SDK ( JAVA ) on a raspberry PI the CPU usage shoots up to 100%. As soon as I kill the process the CPU usage drops back to normal
Below is the code snippet using to start the kaa where i am starting the kaa client on a raspberry pi
public class NotificationSystemTestApp {
private static final Logger LOG = LoggerFactory.getLogger(NotificationSystemTestApp.class);
public static void main(String[] args) throws InterruptedException {
/*System.out.println(encryptString("abcd", "12332sd1133sdssd45"));
return;*/
new NotificationSystemTestApp().launch();
}
private void launch() throws InterruptedException {
// Create client for Kaa SDK
final KaaClient kaaClient;
DesktopKaaPlatformContext desktopKaaPlatformContext = new DesktopKaaPlatformContext();
final CountDownLatch startupLatch = new CountDownLatch(1);
kaaClient = Kaa.newClient(desktopKaaPlatformContext, new SimpleKaaClientStateListener() {
#Override
public void onStarted() {
LOG.info("--= Kaa client started =--");
startupLatch.countDown();
}
#Override
public void onStopped() {
LOG.info("--= Kaa client stopped =--");
}
}, true);
kaaClient.setProfileContainer(new ProfileContainer() {
public ClientProfile getProfile() {
return new ClientProfile() {{
setClientProfileInfo(new ProfileInfo() {{
setRidlrId("R_00001");
setStationName("Mumbai");
setEquipmentId("EQ0006");
setStationId("5");
}});
}};
}
});
// Registering listener for topic updates
kaaClient.start();
startupLatch.await();
kaaClient.addTopicListListener(new NotificationTopicListListener() {
public void onListUpdated(List<Topic> topicList) {
System.out.println("Topic list updated!");
for (Topic topic : topicList) {
LOG.info("Received topic with id {} and name {}", topic.getId(), topic.getName());
}
}
});
final ScanInfo scanInfo = new ScanInfo() {{
setDestinationId("12");
setSourceId("3");
setEquipmentId("R_00001");
setScanTime(System.currentTimeMillis() / 1000);
setEvent("ENTRY");
setTransactionId(UUID.randomUUID().toString());
}};
kaaClient.attachEndpoint(new EndpointAccessToken("1234"), new OnAttachEndpointOperationCallback() {
#Override
public void onAttach(SyncResponseResultType result, EndpointKeyHash resultContext) {
}
});
kaaClient.attachUser("user1", "1234", new UserAttachCallback() {
public void onAttachResult(UserAttachResponse response) {
System.out.println("Attach User Success - " + response.getResult());
}
});
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
LOG.error("FATA", e);
}
LOG.debug("End Point key hash - " + kaaClient.getEndpointKeyHash());
while (true) {
kaaClient.addLogRecord(new LoggerSchema() {{
setData("");
setMessageType("");
}});
Thread.sleep(10);
}
}
}
Thanks,
Rizwan
As I see, you use constant addition of log records for upload to the Kaa server. The delay is just 10 milliseconds which might be too short for the Raspberry PI system you are running the application on.
Depending on the configuration, the Kaa Client might add considerable processing overhead for each log record with processing in other Java threads causing CPU to constantly spin adding and processing new records.
Try increasing the delay in your 'while (true)' loop and check the CPU usage with different log upload settings.
Should this information be not enough for you to fix the issue, please add logs from the Kaa client and Kaa server for investigation.

Message Driven Bean listen to JMS remote destinations (Weblogic)

Greeting, I'm new to Java EE and especially message driven bean so i followed the tutorial which has been working with internal JMS destinations (e.g. weblogic server that runs on my local machine). Now I'm trying to listen to new messages from remote destinations (e.g. another weblogic server run on different machine). I'm thinking about jndi naming lookup however I don't see any appropriate place to implement in the MDB. My question is do I need any config files in order for this to work? or is it even possible to listen to remote JMS destinations?
#MessageDriven(mappedName="jms/myQueue") //jms/myQueue is remote queue name
public class PMQueueListener implements MessageListener{
#Resource
private MessageDrivenContext mdc;
/**
* Default constructor.
*/
public PMQueueListener() {
System.out.println("This is onmessage()");
}
/**
* #see MessageListener#onMessage(Message)
*/
public void onMessage(Message message) {
TextMessage msg = null;
try
{
if (message instanceof TextMessage)
{
msg = (TextMessage) message;
System.out.println("MESSAGE BEAN: Message received: " +
msg.getText());
}
else
{
System.out.println("Message of wrong type: " +
message.getClass().getName());
}
}
catch (JMSException e)
{
e.printStackTrace();
mdc.setRollbackOnly();
}
catch (Throwable te)
{
te.printStackTrace();
}
}
}
You can add below annotation on your MDB and see if it works for you.
#TransactionManagement(TransactionManagementType.CONTAINER)
#TransactionAttribute(TransactionAttributeType.REQUIRED)

redirect java.util.logging to log4j birt

I have a spring mvc webapp which uses BIT reports. BIRT reports uses java.util.logging. I am looking for a way to redirect the java.util.logging to log4.
The instructions at this link detail how to do it but I'm having some troubles getting the solution to work.
http://wiki.eclipse.org/BIRT/FAQ/Deployment#Q%3a_Can_I_use_Log4j_with_BIRT.3F
I've added the class as described. Then I added logging.properties into my webapp /resources directory with the following contents.
handlers=com.myer.reporting.logging.Log4jHandler
I don't think the system parameter applies since I am using a webapp?
Anyway I know it is not working because in my application logging directory I get the standard birt logs every time the application server re-starts.
Can someone help me with clarifying these instructions.
thanks
I don't believe those instructions are acceptable for web application. Where there is a single JUL for the whole web server / servlet container.
If you generate report using BIRT API, BIRT allows you to set your own logger. See the method EngineConfig.setLogger(). It works at least in BIRT 4.3.1. We do as follows (it redirects BIRT logs to SLF4J, but the idea should be the same for Log4J):
EngineConfig config = new EngineConfig();
// set logger that forwards log messages to SLF4J
// as of BIRT 4.3.1 the custom logger will be accepted only if it or one of its parent in the BIRT's root logger list.
// see http://git.eclipse.org/c/birt/org.eclipse.birt.git/commit/engine/org.eclipse.birt.report.engine/src/org/eclipse/birt/report/engine/api/impl/EngineLogger.java?h=Kepler&id=1cb9507c8ce997bf5407a73d9c23487cef002fa9
java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger("org.eclipse.birt" + ".myapp.Slf4jBridge");
julLogger.setUseParentHandlers(false);
Handler logHandler = new Slf4jLogHandler();
logHandler.setLevel(Level.FINEST);
julLogger.addHandler(logHandler);
config.setLogger(julLogger);
And the Slf4jLogHandler is implemented as follows:
public class Slf4jLogHandler extends Handler {
private Formatter julFormatter = new SimpleJulFormatter();
#Override
public void publish(LogRecord record) {
if (record == null) {
return;
}
ClassLoader hanlderClassLoader = this.getClass().getClassLoader();
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (hanlderClassLoader != contextClassLoader) {
// do not log in foreign contexts
/*
* This check is necessary if several web applications with "JUL to SLF4J" bridge are deployed in the same Servlet container.
* Each application has its own instance of SLF4J logger, but they all are mapped to the same JUL logger,
* because the JUL logger is loaded by the root classloader. Whereas SLF4J loggers are loaded by their respective
* webapp classloaders. Thus comparing classloaders is the only known way to find out whom the JUL log record belongs to.
*/
return;
}
String loggerName = record.getLoggerName();
if (loggerName == null) {
loggerName = "unknown";
}
Logger slf4jLogger = LoggerFactory.getLogger(loggerName);
/*
* JUL levels in descending order are:
* <ul>
* <li>SEVERE (highest value)
* <li>WARNING
* <li>INFO
* <li>CONFIG
* <li>FINE
* <li>FINER
* <li>FINEST (lowest value)
* </ul>
*/
if (record.getLevel().intValue() <= Level.FINEST.intValue()) {
if (slf4jLogger.isTraceEnabled()) {
slf4jLogger.trace(julFormatter.format(record), record.getThrown());
}
} else if (record.getLevel().intValue() <= Level.FINE.intValue()) {
if (slf4jLogger.isDebugEnabled()) {
slf4jLogger.debug(julFormatter.format(record), record.getThrown());
}
} else if (record.getLevel().intValue() <= Level.INFO.intValue()) {
if (slf4jLogger.isInfoEnabled()) {
slf4jLogger.info(julFormatter.format(record), record.getThrown());
}
} else if (record.getLevel().intValue() <= Level.WARNING.intValue()) {
if (slf4jLogger.isWarnEnabled()) {
slf4jLogger.warn(julFormatter.format(record), record.getThrown());
}
} else if (record.getLevel().intValue() <= Level.SEVERE.intValue()) {
if (slf4jLogger.isErrorEnabled()) {
slf4jLogger.error(julFormatter.format(record), record.getThrown());
}
} else if (record.getLevel().intValue() == Level.OFF.intValue()) {
// logger is switched off
} else {
slf4jLogger.warn("Unexpected log level {}.", record.getLevel().intValue());
if (slf4jLogger.isErrorEnabled()) {
slf4jLogger.error(julFormatter.format(record), record.getThrown());
}
}
}
#Override
public void flush() {
// noop
}
#Override
public void close() throws SecurityException {
// noop
}
}
This approach works even if there are several web apps using BIRT on the same server.
I use a ServletContextListener, that install SLF4JBridgeHandler as unique handler for j.u.l. logger:
public class InstallJULHandlerListener implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent arg0) {
// Optionally remove existing handlers attached to j.u.l root logger
SLF4JBridgeHandler.removeHandlersForRootLogger(); // (since SLF4J 1.6.5)
// add SLF4JBridgeHandler to j.u.l's root logger, should be done once
// during the initialization phase of your application
SLF4JBridgeHandler.install();
}
#Override
public void contextDestroyed(ServletContextEvent arg0) {
}
}

Why Java servlet can't get Paypal IPN messages everytime?

I have a Java servlet running on my notebook with Windows Vista, I set up a static IP, did port forwarding and registered for a free DDNS service, now my servlet is running, I gave the url to Paypal to send me IPN messages, I went on to it's sandbox site got to the test tools page, tried to send test messages by clicking the "Send IPN" button, most of the time it would fail, the error is : "IPN delivery failed. Unable to connect to the specified URL. Please verify the URL and try again."
But maybe 1 in 10 times, it might be successful and my servlet would get the message, and I looked at the messages I got, they are in correct format. So I called Paypal asking why, he said I shouldn't run the servlet on my notebook, in stead I should run it on the web server, but I told him my ISP doesn't support Java on their server, and since I did all the above steps, shouldn't it be the same to run the servlet on my notebook ? He said his test showed he couldn't get to my servlet, but I asked why maybe 1 in 10 times it could get through ? If there is something wrong with running it on my notebook, then 100% times it should fail, am I correct on this point ? But anyway he said that's all he could do, and I should troubleshoot it myself. The servlet looks like this :
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class PayPal_Servlet extends HttpServlet
{
static boolean Debug=true;
static String PayPal_Url="https://www.paypal.com/cgi-bin/webscr",Sandbox_Url="https://www.sandbox.paypal.com/cgi-bin/webscr",
Dir_License_Messages="C:/Dir_License_Messages/";
static TransparencyExample Transparency_Example;
static PayPal_Message_To_License_File_Worker PayPal_message_to_license_file_worker;
// Initializes the servlet.
public void init(ServletConfig config) throws ServletException
{
super.init(config);
if (!new File(Dir_License_Messages).exists()) new File(Dir_License_Messages).mkdirs();
System.gc();
}
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* #param request servlet request
* #param response servlet response
*/
protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
// Read post from PayPal system and add 'cmd'
Enumeration en=request.getParameterNames();
String str="cmd=_notify-validate";
while (en.hasMoreElements())
{
String paramName=(String)en.nextElement();
String paramValue=request.getParameter(paramName);
str=str+"&"+paramName+"="+URLEncoder.encode(paramValue);
}
// Post back to PayPal system to validate
// NOTE: change http: to https: in the following URL to verify using SSL (for increased security).
// using HTTPS requires either Java 1.4 or greater, or Java Secure Socket Extension (JSSE) and configured for older versions.
URL u=new URL(Debug?Sandbox_Url:PayPal_Url);
URLConnection uc=u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
PrintWriter pw=new PrintWriter(uc.getOutputStream());
pw.println(str);
pw.close();
BufferedReader in=new BufferedReader(new InputStreamReader(uc.getInputStream()));
String res=in.readLine();
in.close();
// Assign posted variables to local variables
String itemName=request.getParameter("item_name");
String itemNumber=request.getParameter("item_number");
String paymentStatus=request.getParameter("payment_status");
String paymentAmount=request.getParameter("mc_gross");
String paymentCurrency=request.getParameter("mc_currency");
String txnId=request.getParameter("txn_id");
String receiverEmail=request.getParameter("receiver_email");
String payerEmail=request.getParameter("payer_email");
if (res.equals("VERIFIED")) // Check notification validation
{
// check that paymentStatus=Completed
// check that txnId has not been previously processed
// check that receiverEmail is your Primary PayPal email
// check that paymentAmount/paymentCurrency are correct
// process payment
}
else if (res.equals("INVALID")) // Log for investigation
{
}
else // Log for error
{
}
// ===========================================================================
if (txnId!=null)
{
Write_File_Safe_Fast(Dir_License_Messages+txnId+".txt",new StringBuffer(str.replace("&","\n")),false);
}
// ===========================================================================
String Message_File_List[]=Tool_Lib.Get_File_List_From_Dir(Dir_License_Messages);
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String title="Reading All Request Parameters",Name="",Value;
out.println("<Html><Head><Title>"+title+"</Title></Head>\n<Body Bgcolor=\"#FDF5E6\">\n<H1 Align=Center>"+title+"</H1>\n"+
"<Table Border=1 Align=Center>\n"+"<Tr Bgcolor=\"#FFAD00\"><Th>Parameter Name</Th><Th>Parameter Value(s) Messages = "+Message_File_List.length+"</Th></Tr>");
Enumeration paramNames=request.getParameterNames();
while(paramNames.hasMoreElements())
{
String paramName=(String)paramNames.nextElement();
out.print("<Tr><Td>"+paramName+"</Td><Td>");
String[] paramValues=request.getParameterValues(paramName);
if (paramValues.length == 1)
{
String paramValue=paramValues[0];
if (paramValue.length() == 0) out.print("<I>No Value</I>");
else
{
out.println(paramValue+"</Td></Tr>");
// Out("paramName = "+paramName+" paramValue = "+paramValue);
// if (paramName.startsWith("Name")) Name=paramValue;
// else if (paramName.startsWith("Value")) Write_File_Safe_Fast("C:/Dir_Data/"+Name,new StringBuffer(paramValue),false);
}
}
else
{
out.println("<Ul>");
for (int i=0;i<paramValues.length;i++) out.println("<Li>"+paramValues[i]);
out.println("</Ul></Td</Tr>");
}
}
out.println("</Table>\n</Body></Html>");
}
/** Handles the HTTP <code>GET</code> method.
* #param request servlet request
* #param response servlet response
*/
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { processRequest(request,response); }
/** Handles the HTTP <code>POST</code> method.
* #param request servlet request
* #param response servlet response
*/
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { processRequest(request,response); }
// Returns a short description of the servlet.
public String getServletInfo() { return "Short description"; }
// Destroys the servlet.
public void destroy() { System.gc(); }
public static void Write_File_Safe_Fast(String File_Path,StringBuffer Str_Buf,boolean Append)
{
FileOutputStream fos=null;
BufferedOutputStream bos=null;
try
{
fos=new FileOutputStream(File_Path,Append);
bos=new BufferedOutputStream(fos);
for (int j=0;j<Str_Buf.length();j++) bos.write(Str_Buf.charAt(j));
}
catch (Exception e) { e.printStackTrace(); }
finally
{
try
{
if (bos!=null)
{
bos.close();
bos=null;
}
if (fos!=null)
{
fos.close();
fos=null;
}
}
catch (Exception ex) { ex.printStackTrace(); }
}
System.gc();
}
}
I use Netbean6.7 to develop the servlet, and the code was from Paypal's JSP sample code, what can I do to debug the problem ?
HI, try to use my library:
http://paypal-nvp.sourceforge.net/index.htm
I hope it will help you. If you have any questions, improvements you can contact me. You find my email in the comments of the source.

Resources