I done some java bytecode instrumentation with -javaagent argument and premain method. But this is the first time i hear about agentmain method. I have some questions about this method. Here follows it.
Both premain and agentmain method have same use?
When agentmain method invoked?
What is the use of agentmain method in java instrumentation?
premain is invoked when an agent is started before the application. Agents invoked using premain are specified with the -javaagent switch.
agentmain is invoked when an agent is started after the application is already running. Agents started with agentmain can be attached programatically using the Sun tools API (for Sun/Oracle JVMs only -- the method for introducing dynamic agents is implementation-dependent).
An agent can have both a premain and an agentmain, but only one of the two will be called in a particular JVM invocation. In other words, your agent will either start with premain or agentmain, but not both.
You can find more information about this in the answer to the question Starting a Java agent after program start.
Related
I'm confused a little: AFAIK VisualVM perform profiling and sampling, so does it mean it not only makes dumps (thread stacks + memory state) but also instrumenting the code?
From here: https://stackoverflow.com/a/12130149/10894456 explained the profiling implies instrumenting. But does VisualVM makes instrumenting by itself or need something to prepare (like Java Agent or something)?
Yes, when you use the Profiler, VisualVM will instrument the bytecode as necessary. This can only be done via an Agent, so VisualVM includes such a Java Agent. When you are connected to a JVM on the same machine, it may use the Attach API to load the Agent into the target JVM dynamically. So in this use case, it doesn’t need additional preparation steps on the user’s side.
I have a C++ COM (multi-threaded apartment model) application that I build with Visual C++ 2019. I am playing with the COM-based Active Script interfaces and classes. I have a JavaScript ("JScript" really, CLSID {F414C260-6AC0-11CF-B6D1-00AA00BBBB58}) Active Script COM object created and set up. Everything works surprisingly well (for something that is falling into obscurity, unfortunately).
Is there an idiomatic approach to registering and calling event listeners where a script can use an API like like https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener and where I can dispatch events from the script host (C++)? To clarify, I am well able to call IDispatch interface methods from scripts, I want to call script functions that were passed earlier with such methods to the program, from the program. GetScriptDispatch is something else -- it lets me call methods available in the global script namespace, but I need to call functions that I pass to a proxied (from the script) "addEventListener" method.
I depend on this functionality because I want to dispatch events. Basically I want addEventListener to be available on certain proxied (e.g. objects added to script namespace with AddNamedItem) objects and I need to call event listeners added through calls to that method, when certain events in the C++ application happen.
I have read something about IConnectionPoint, and the Active Script documentation briefly mentions "event sinks" a couple of times, without there being examples on how and for what these can be used for. There is also the IBindEventHandler interface, without much explanation again as to what to use it for.
I understand this is ancient stuff, but I like COM and Active Script interface has several implementations, which to me adds utility.
You need to create an object that implements events (see IConnectionPoint).
After starting the script you can call AddNamedItem. The script now knows the interface of you object. If it implements it can register to this events when the state changes to SCRIPTSTATE_CONNECTED.
If an event is fired, the script will receive the event and will execute the appropriate code section.
There are old samples:
Q168214 SAMPLE: MFCAXS (you might find it in the wayback engine)
And there is a sample here in code project
For ATL samples for Events look here
I'm writing a Gradle plugin that interacts with an external HTTP API. This interaction is handled by a single class (let's call it ApiClient). I'm writing some high-level tests that use Gradle TestKit to simulate an entire build that uses the plugin, but I obviously don't want them to actually hit the API. Instead, I'd like to mock ApiClient and check that its methods have been called with the appropriate arguments, but I'm not sure how to actually inject the mocked version into the plugin. The plugin is instantiated somewhere deep within Gradle, and gets applied to the project being executed using its void apply(Project project) method, so there doesn't appear to be a way to inject a MockApiClient object.
Perhaps one way is to manually instantiate a Project, apply() the plugin to it (at which point, I can inject the mocked object because I have control over plugin instantiation), and then programmatically execute a task on the project, but how can I do that? I've read the Gradle API documentation and haven't seen an obvious way.
A worst-case solution will be to pass in a debug flag through the plugin extension configuration, which the plugin will then use to determine whether it should use the real ApiClient or a mock (which would print some easily grep-able messages to the STDOUT). This isn't ideal, though, since it's more fuzzy than checking the arguments actually passed to the ApiClient methods.
Perhaps you could split your plugin into a few different plugins
my-plugin-common - All the common stuff
my-plugin-real-services - Adds the "real" services to the model (eg RealApiClient)
my-plugin-mock-services - Adds "mock" services to the model (eg MockApiClient)
my-plugin - Applies my-plugin-real-services and my-plugin-common
my-plugin-mock - Applies my-plugin-mock-services and my-plugin-common
In the real world, people will only ever apply: 'my-plugin'
For testing you could apply: 'my-plugin-mock'
I am trying to migrate a legacy app that is using camel/cxf (offers some web services that include transformations) to Websphere Liberty 16.0.0.03 (IBM JRE 1.8). Tests are failing because the app uses extensions functions. I tried to disable secure processing as described here.
This change has no effect. That's why I try to switch to Saxon Implementation globally by setting System Property "javax.xml.transform.TransformerFactory=net.sf.saxon.TransformerFactoryImpl" in jvm.options config file. Again - this does not work.
While debugging I can see, that com.ibm.ws.webcontainer.osgi.mbeans.PluginGenerator$2 is overriding the Property with com.ibm.xtq.xslt.jaxp.compiler.TransformerFactoryImpl during Server start. I can see a method "PluginGenerator.revertTransformerFactoryIfNeccessary" in the stack that seems to trigger the change. Afterwards all FactoryFinder.find() will return the non-Saxon implementation.
Can anyone suggest how to either disable secure-processing successfully
or
a way to successfully set a custom TransformerFactory?
BTW: It seems to me like these 2 are bugs - do I report these as regular PMR?
EDIT: possible workaround
As result of the helpful suggestions I added an '#WebListener' that will sets the System Property within the constructor (setting it in contextInitialized is too late as stylesheets seem to be compiled during application start and thus processing fails tests). I bundle this a "patch-jar" with the legacy app.
The Liberty web container plugin generator will only override the xml transformer factory if the IBM JDK is being used.
While the web container performs plugin generation using the IBM JDK, it will swap to an alternate transformer factory, and then reset to the IBM JDK default of which is com.ibm.xtq.xslt.jaxp.compiler.TransformerFactoryImpl.
I think it is worth opening a PMR here. The PluginGenerator should not assume that it started with the default xml transformer factory, and should instead save off the value of javax.xml.transform.TransformerFactory and then restore it after plugin generation has completed.
Temp workaround:
Since the PluginGenerator only swaps the XML transformer factory if you're running on the IBM JDK, you could change to an alternate JDK until your PMR gets resolved.
I agree that this is a bug. The official route for reporting problems is a PMR, but there is enough here for us to understand the problem and fix it through our beta program. If you want to get an iFix on a released version of the product (rather than waiting for it to come out via the beta program) then you will need to raise a PMR.
I'm reading through a tutorial on using voice commands to control applications and, in an example of controlling rhythmbox, it suggests commands such as the following can be executed:
rhythmbox-client --play
rhythmbox-client --pause
Why does this not simply open a new instance of the program, and how can I emulate the functionality in my own programs? For example, how could I pass a string to a particular instance of a program?
Thanks
Rhythmbox uses inter-process communictation to achieve this type of functionality, and this can be implemented in a number of different ways. One of them is to use D-Bus, like Rhythmbox does.
Using D-Bus is not very easy, but the basic idea is that you register your application in D-Bus, so other applications can call different procedures your app exports (for example play/stop actions), and then in the same application implement a client. This way, if arguments like --play are passed, you don't run the usual code, but just check for an instance of the running app and send a command to your already running program.
On the other hand, when no arguments are passed, your program just starts and registers the proper triggers, so that a later called instance can control it.
Here is a tutorial on dbus, and the DBus homepage
There are several techniques to have only one application instance running. In these terms calling yyy --play would generally mean the same as
INSTANCE = GET_RUNNING_INSTANCE()
IF INSTANCE == NULL
INSTANCE = CREATE_NEW_INSTANCE()
SEND_MESSAGE(INSTANCE, PLAY)
For example, how could I pass a string to a particular instance of a program?
You'll need to use whatever interprocess communication facilities your operating system offers. Sockets or named pipes, for example, or messages.